C Program to Find Factorial Using Loops,Recursion
This is the implementation of factorial program in c,using recursion and loops
USING FOR LOOP:
#include<stdio.h>
main()
{
int i;
int f=1;
int n;
printf("Enter any number n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
f=f*i;
}
printf("FACTORIAL IS %d n",f);
}
USING RECURSION FUNCTION:
#include<stdio.h>
main()
{
int n;
printf("Enter a Number n");
scanf("%d",&n);
int f;
f=fact(n);
printf("%d",f);
}
int fact(int n)
{
int factorial;
if (n==1){
printf("nFacorial %d",factorial);
return (1);
printf("n %d",factorial);
}
else
factorial=n*fact(n-1);
printf("n FACTORIAL %d",factorial);
return(factorial);
}
USING WHILE LOOP:
#include<stdio.h>
main()
{
int num;
int i=1;
int fact=1;
printf("Enter a Number n");
scanf("%d",&num);
while (i!=num)
{
i++;
fact=fact*i;
}
printf("The factorial is %dn",fact);
}
Recent Comments