20_10

Write a C program to calculate factorial of a number using recursion.

 Source code:

//Write a C program to calculate factorial of a number using recursion.


#include<stdio.h>

int factorial(int n)

{

    if(n==0)

        return 1;

    else

        return (factorial(n-1)*n);

}

int main()

{

    int num,f;

    printf("Enter a number: ");

    scanf("%d",&num);/*Enter the number whose factorial to be found*/

    f=factorial(num); 

/*Write the factorial(num) function which calculates the function in recursive method and returns f */

    printf("Factorial of %d = %d",num,f);

    return 0;

}

Output:
C program to calculate factorial of a number using recursion


No comments:

Post a Comment

Write a program in C to convert a decimal number to binary using recursion.

 Source code: //Write a program in C to convert a decimal number to binary using recursion. #include<stdio.h> long convertB_to_D(int d...