Factorial of a number

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..
 
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)
 
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:
 
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 :( ...
 
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();

}
 
#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
 
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...
 
\/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 ^^ ?
 
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
 
why not just use recursive callbacks, thats the way to do it.

EDIT: oops thunderbolt has posted that already :D

sorry i just posted based on 1st page :) nevermind..
 
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 ... ^^ :( ..
 
Back
Top