- Thread starter Keane 16
- Start date

actly it was sumthin like this:

7 factorial = 7 X 6 X 5 X 4 X 3 X 2 X1

i am not sure..if this is wht u want...and not even sure if what i said is right..

sorry..if i confused u more..

while programming these kinda mathmatics...get to know the method involved & write accrding your logic..

for instance..

factorial of 5 is 5 *4 *3 *2*1

fact of 4 is 4*3*2*1

fact of 3 is 3*2*1

what can u make out of these above...?

simple multiply a int by (int-1) int >0 thats it..

( not sure abt syntax.... of c language)int sol;

sol=1;

if (factno<1)

exit();

for(i=factno;i>0;i--)

{sol=sol*i;

}

printf("The factorial of %d is %d ",factno,sol);

^^use the logic in a function or even in a single main program...it shdnt be more than a few lines..

#include <stdio.h>

#include <conio.h>

void main()

{

int product=1,a,i;

printf("enter the no.\n");

scanf("%d",&a);

for(i=1;i<=a;i++)

{

product=product*i;

}

printf("factorial is %d",product);

getch();

}

It wont work re... cin the input first...akshitmohan said:int a,b,i;

cout<<"Enter the number whose factorial is to be calculated -";

for(i=a; i>=1; i--)

{

b=a*i;

But doesn't work ...

Get the input

cin>>a;

Then it should work...

What in the world does that line do ^^ ?\/endett/\ said:#include <iostream.h>

#include <conio.h>

void main ()

{

int a,b=1;

cout<<"Enter the number whose factorial is to be calculated -";

cin>>a;

for(a;a>1;a--) {

b*=a;

}

cout<<endl<<b;

getch();

}

This works on BC++ 4.5

Here's how you can do it...

**************************

#include<stdio.h>

int fact(int i)

{

if(i==1)

return 1;

else

return (i*fact(i-1));

}

int main()

{

printf("%d",fact(1));

printf("%d",fact(5));

return 1;

}

**********************

It expands as

eg : fact(4)

fact (4) // i = 4 != 1 so return 4*fact(4-1)

4 * fact(3)

4 * 3 * fact(2)

4 * 3 * 2 * fact(1) // i = 1 so return 1

4 * 3 * 2 * 1

EDIT: oops thunderbolt has posted that already

sorry i just posted based on 1st page nevermind..

What does the part in bold do ? Will the prog work w/o it ?#include<stdio.h>

int fact(int i)

{

if(i==1)

return 1;

else

return (i*fact(i-1));

}

int main()

{

printf("%d",fact(1));

printf("%d",fact(5));

return 1;

}

That Didn't work ... ^^ ..