Menu

Add Topic

Forum

waiting answer January 04, 2021

Write a c program to find factorial of a number

Answers
January 04, 2021

There are different ways in the C program to find the factorial of a number. Let's see how to write a c program to find the factorial of a number using C.

Factorial Program using the loop

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

OUTPUT

         Enter a number: 2
         Factorial of the number 2 is: 2    

0 0
January 04, 2021

Factorial Program using recursion in C

    #include<stdio.h>  
  
    long factorial(int num)  
    {  
      if (num == 0)  
        return 1;  
      else  
        return(num * factorial(num-1));  
    }  
       
    void main()  
    {  
      long fact;    
      int number; 
      printf("Enter a number: ");  
      scanf("%d", &number);   
      fact = factorial(number);  
      printf("Factorial of the number %d is %ld\n", number, fact);  
      return 0;  
    }

OUTPUT

        Enter a number: 5
        Factorial of the number 5 is 120

0 0

Please Login to Post the answer

Leave an Answer