Factorial Program in C / Find factorial using Recursion

Factorial Program in C / Find factorial using Recursion
Education

What is Factorial?

In Mathematics, factorial is a simple thing. Factorials are just products. An exclamation mark indicates the factorial. Factorial is a multiplication operation of natural numbers with all the natural numbers that are less than it. In this article, let’s discuss the factorial definition, formula and examples.

The formula to find the factorial of a number is

n! = n × (n-1) × (n-2) × (n-3) × ….× 3 × 2 × 1

Factorial of 10

For example, the factorial of 10 is written as

10! = 10 (9 × 8 × 7 × 6 × 5× 4 × 3 × 2 × 1)

Therefore, the value of 10 factorial is 3,628,800


How to find factorial in C using loog

    #include  
    int main()    
    {    
     int i,fact=1,number;    
     printf("Enter a number: ");    
      scanf("%d",&number);    
        for(i=1;i<=number;i++){    
          fact=fact*i;    
      }    
      printf("Factorial of %d is: %d",number,fact);    
    return 0;  
    }  

Find factorial in C using Recursion

#include
int main(){
int n;
long int fact(int n);
printf("Enter Your number :");
scanf("%d",&n);

printf("The factorial of %d is %ld",n,fact(n));
return 0;
}

long int fact(int n){
       if(n>=1){
             return n*fact(n-1);
        }else{
             return 1;
       }
}

OUTPUT :

Enter a number: 5
Factorial of 5 is: 120


CONCLUSION :

There are multiple methods to calculate the factorial of a number in C. Whether it's using an iterative solution, recursion, the tgamma() method, or pointers, the most suitable method will depend on the specific needs of your program.




  • To Share this Blog, Choose your plateform


Write your Testimonial

Your review is very precious for us.


Rating: