Find Factorial

Learn and Trouble Shoot Problems Related to C-Language here.

Find Factorial

Unread postby rajeevmahura » Thu May 22, 2008 6:37 pm

#include<stdio.h>
#include<conio.h>
factor(int);
void main()
{
int fact,c;
clrscr();
printf("Enter the integar");
scanf("%d",&fact);
c=factor(fact);
printf("%d",c);
getch();
}
int factor(int n)
{
int i,prod=1;
if(n>1)
for(i=2;i<=n;i++)
prod*=i;
return(prod);
}
--Regards--
Rajeev Mahura
Servers & Storage Professional
http://about.me/rajeevmahura
User avatar
rajeevmahura
 
Posts: 144
Joined: Tue May 20, 2008 5:22 pm
Location: Bangalore | New Delhi
Full Name: Rajeev Mahura
Date Of Birth: 15 Jan 1984
Address: New Delhi
Profession: DOEACC Trainer
DOEACC Center: DOEACC DELHI
Mobile No: 0

Re: Find Factorial

Unread postby manojthesingham » Tue Sep 01, 2009 6:05 am

/*finding factorial recursively*/
#include<stdio.h>
#include<conio.h>

int main(int argc,char**argv)
{
int x;
printf("Enter the Number to find its Factorial : ");
scanf("%d",&x);
printf("%d",factorial(x));
getch();
return 0;
}
/*recursive factorial method*/
/*note : int function(int a,int b,......,int z) does not need function prototype*/
int factorial (int x)
{
if(x<=1)
return 1;
return x*factorial(x-1);
}
/* how it works :- */
/* tracing factorial(4)*/
/*STACK operation*/
/* --------------------------*/
/* | 2 * factorial(1)=2*1 |-------------->TOP of STACK(Activation Record)*/
/* --------------------------*/
/* | 3 * factorial(2)=3*2 |*/
/* --------------------------*/
/* | 4 * factorial(3)=4*6 |*/
/* --------------------------*/
User avatar
manojthesingham
 
Posts: 15
Joined: Tue Sep 01, 2009 5:39 am
Location: Coimbatore,Tamilnadu
Full Name: manoj
Date Of Birth: 31 Jul 1989
Address: Coimbatore18Tamilnadu
Mobile No: 0


Return to C-Language

Who is online

Users browsing this forum: No registered users and 2 guests

cron