Home Uncategorized Factorial Program in C

Factorial Program in C

SHARE

Write a C Program to find Factorial of a given number using functions without Recursion.

#include<stdio.h>
long int fact(int);
void main()
{
   int n; //For read user value.
   long int r; //to Store the result value.
   printf(“Enter a number : “);
   scanf(“%d”,&n);
   if(n<=0) //Checking entered value is valid or not
   {
      printf(“Please enter a valid number\n”);
      return;
   }
   r=fact(n); //Function calling and store the resultent value
   printf(“Factorial of given number is : %ld\n”,r);
   return;
}
long int fact(int a)
{
   int i;
   long int fac=1;
   for(i=1;i<=a;i++)
   fac=fac*i;
   return fac; //Returning the result value
}
Output:
Enter a number : 6
Factorial of given number is : 720

Factorial of a given number using functions with Recursion.

Back to the programs list.