# Factorial of a number

#### Keane 16

##### New Member
Disciple
Exam dayafter. How do i WAP to get the factorial of a number ?????

#### FaH33m

##### Analog Vibes
Veteran
i dunno much about..this...cauz i am just in 11th..now..but i had heard something abt factorial..

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..

##### Well-Known Member
which language do u want it in.??

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..

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);
( not sure abt syntax.... of c language)

#### Keane 16

##### New Member
Disciple
Turbo C++ man ........

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

#### hunt3r

##### Well-Known Member
You cant even do a factorial program by urself? :O U sure u made the right choice of science?

#### Keane 16

##### New Member
Disciple
Lol. Just because i took science doesn't mean i'll bloody become a C++ expert all of a sudden.

That post was just a waste of your and my time. :cool2:

#### Keane 16

##### New Member
Disciple
Here's what i've got -

#include <iostream.h>

#include <conio.h>

void main ()

{

int a,b,i;

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

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

{

b=a*i;

cout<<b<<endl;

}

getch();

}

But doesn't work ...

#### Rahul

##### Well-Known Member
here u go hope this will work ..... i don't have a Turbo C so can't chk

#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();

}

#### -nsan-ty

##### New Member
Disciple
#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

#### Josh

##### Well-Known Member
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 ...
It wont work re... cin the input first...
Get the input
cin>>a;

Then it should work...

#### Keane 16

##### New Member
Disciple
\/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
What in the world does that line do ^^ ?

#### zhopudey

##### Well-Known Member
it means b = b*a;

#### Keane 16

##### New Member
Disciple
ahh.ok. 10 chars.

Disciple

#### Keane 16

##### New Member
Disciple
Ahh, i get why he's used a>1 cause =1 doesn't make a diff ..

But why "a" and not a<b ?

#### Deus Ex Machina

##### Active Member
Factorial is a classic case for usage of recursion.
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

#### Amol

##### Active Member
^ excellent reply there. Didnt know there was a function for factorials in C++

Functions make the job a lot easier.

#### Safin

##### Well-Known Member
Super Mod
why not just use recursive callbacks, thats the way to do it.

EDIT: oops thunderbolt has posted that already

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

#### Keane 16

##### New Member
Disciple
Nice reply, but i didnt get one thing -

#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;

}
What does the part in bold do ? Will the prog work w/o it ?