Find factorial of a given number using the recursive function method in C

Write a program to find factorial of a given number using the recursive function method in C

Hints:

Factorial !

Example: 4! is shorthand for 4 x 3 x 2 x 1
Factorial Symbol The factorial function (symbol: !) means to multiply a series of descending natural numbers. Examples:
  • 4! = 4 × 3 × 2 × 1 = 24
  • 7! = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5040
  • 1! = 1

Source Code:

#include<stdio.h>
//function declaration before the main
int factorial(int n);
//main function of the program
int main()
{
    int n;
    scanf("%d",&n);
    //call function and print the final value at a time
    printf("%d", factorial(n));
    return 0;
}
int factorial(int n)
{
    if(n!=1)
     return n*factorial(n-1);
}

1 comment:

  1. #include
    int main()
    {
    int r;
    scanf("%d",& r);
    printf("%d",fact(r));
    return 0;
    }
    int fact(int r)
    {
    if(r!=1)
    return r*fact(r-1);

    }

    ReplyDelete