Saturday, 29 January 2022

Factorial Program in C full explanation :

 **Factorial Program in C :- 

Factorial program in C: Factorial of n is the product of all positive descending integers. Factorial of  n is denoted by n!. 

 *For example :    (i)  5! = 5*4*3*2*1 = 120

                              (ii)  3! = 3*2*1 = 6

            Here, 5! is pronounced as "5 factorial", it is also called "5 bang" or "5 shriek". The factorial is normally used in mathematics.

           There are many way to write the factorial program in C language. Let's see the 2 ways to  write the factorial program. 

*(i) Factorial Program using loop :-

Main source :-

  #include <stdio.h>

  int main()

 {

  int a, i, f=1;

  printf("\nenter a number to calculate the factorial");

  scanf("%d",&a);

  for(i=1;i<=a;i++)

    {

         f= f*i;

         printf("the factorial of%d=%d"a,f);

     }

  return 0;

}

Input :-

Output :-


(ii) Factorial Program using Recursion in C :-

Main source :-

#include<stdio.h>  

  long factorial(int n);

   int main()  

{  

  int number;  

  long fact;  

  printf("Enter a number: ");  

  scanf("%d", &number);   

   fact = factorial(number);  

  printf("Factorial of %d is %ld\n", number, fact);  

  return 0;  

}

long factorial(int n)  

{  

  if (n == 0)  

    return 1;  

  else  

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

}

Input :-



Output :-


  IF YOU HAVE ANY QUARRIES SO YOU CAN DIRECTLY CONTACT WITH US,GO INTO THE CONTACT   INFORMATION PAGE AND THEN TYPE YOUR QUARRIES .

 THANKS FOR VISITING . IF YOU LOVE PROGRAMMING THEN YOU CAN FOLLOW OUR WEBSITE .....

            

1 comment:

Infosys Process Executive Recruitment 2022 | Apply Now

  Infosys Process Executive Recruitment 2022 | Apply Now  Infosys Process Executive Recruitment 2022 |Infosys Recruitment 2022 | Work From H...