Factorial Program using Loops

This Factorial Program using Recursion in C post explains clealry how to find the factorial given input number either by using for loop as well as using recursion.


#include<stdio.h>
void main()
{
int n , i, fact=1 ;
printf(“Enter the Limit : “);
scanf(“%d”, &n);
for(i=1 ; i<=n ; i++)
{
fact = fact*i ;
}
printf(“Factorial value : %d”,fact);
}


Factorial of a given number using Recursion:

#include<stdio.h>
int fact(int);
void main()
{
int n , i, res=1 ;
printf(“Enter the Limit : “);
scanf(“%d”, &n);
res = fact(n);
printf(“Factorial value : %d”,res);
}
int fact(int n)
{
int res ;
if(n==0)
{
res = 1;
}
else
{
res = n*fact(n-1);
}
return res ;
}

Share this