Pages

Ads 468x60px

Saturday, 2 June 2012

C Program For FACTORIAL USING RECURSION


C Program For FACTORIAL USING RECURSION



Factorial program in c: c code to find and print factorial of a number, three methods are given, first one uses a for loop, second uses a function to find 
factorial and third using recursion. Factorial is represented using !, so five factorial will be written as 5!, n factorial as n!. Alson! = n*(n-1)*(n-2)*(n-3)...3.2.1 and zero factorial is defined as one i.e. 0!=1.





CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,f;
clrscr();
printf("Enter the n value:");
scanf("%d",&n);
f=fact(n);
printf("factorial value is=%d",f);
getch();
}
int fact(int a)
{
int c=1;
if(a==1)
{
return(1);
}
else
{
a=a*fact(a-1);
//printf("%d\n",c);
return (a);
}
}

0 comments:

Post a Comment

Total Pageviews