Factorials

n!= n * (n-1) * (n-2) * (n-3)………upto 1.
5!= 5 * (5-1) * (5-2) * (5-3) * 2 * 1 = 120.

This is a recursion process.

#include<stdio.h>
long fac(long x){
if(x==0)
return 1;
return x * fac(x-1);
}
/********************************************************************/
int main(){
long n,res;
while(scanf(“%ld”,& n)==1){
res=fac(n);
printf(“%ld\n”,res);
}
return 0;}

Factorial by normal multiplication:

#include<stdio.h>
long fac(long x){
long i,res=1;
for(i=1;i<=x;i++)
res*=i;
return res;
}
int main(){
long n,res;
while(scanf(“%ld”,&n)==1){
res=fac(n);
printf(“%ld\n”,res);
}
return 0;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s