factorial without recursion in c language:
#include<stdio.h>
int main()
{
int i,number ,fact=1;
printf("enter a number\n");
scanf("%d",&number);
for(i=1;i<=number;++i){
fact=i*fact;
printf("factorial is %d\n\n:",fact);
}return 0;
}
factorial with recursion:
#include<stdio.h>
int factorial(int n);
int main()
{
int n;
printf("enter n:");
scanf("%d",&n);
printf("factorial is :%d", factorial (n));
return 0;
}
int factorial (int n){
if(n==0){
return 1;
}
int factnm1=factorial(n-1);
int factn = factnm1*n;
return factn;
}
Very useful 👍
ReplyDelete