Factorial of a number

FaH33m

Analog Vibes
Veteran
Apr 29, 2006
4,936
255
223
29
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..
 

ComradE_BeaN

Well-Known Member
Adept
Feb 21, 2006
2,432
47
112
www.google.co.in
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)
 

hunt3r

Well-Known Member
Adept
Feb 17, 2006
3,598
49
112
31
You cant even do a factorial program by urself? :O U sure u made the right choice of science?
 

Keane 16

New Member
Disciple
Feb 7, 2006
2,173
16
0
28
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
Feb 7, 2006
2,173
16
0
28
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
Adept
Jun 11, 2005
2,936
150
152
35
Chennai
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
May 10, 2006
342
9
0
#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
Adept
Mar 24, 2006
1,179
24
101
35
www.mobguru.com
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
Feb 7, 2006
2,173
16
0
28
\/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 ^^ ?
 

Keane 16

New Member
Disciple
Feb 7, 2006
2,173
16
0
28
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
Adept
Mar 18, 2005
449
8
32
34
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
Adept
Apr 14, 2005
478
22
32
Boston MA
^ excellent reply there. Didnt know there was a function for factorials in C++

Functions make the job a lot easier.
 

Keane 16

New Member
Disciple
Feb 7, 2006
2,173
16
0
28
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 ?

PLEASE REPLY ASAP.

That Didn't work ... ^^ :( ..