Factorial of a number

kcab

Banned
Sep 15, 2006
63
1
0
C++

simple man
#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();

int a,b;
cin>>a;
b=1;
for(int c=1; c<=a; c++)
{
b=b*c;
}
cout<<"Factorial:"<<b;

getch();
}
1)include etc are the preprocessor directives.
2)void main etc is for begining the program.
3)clrscr(); - clear screen
4)getch(); - to display the result until a key is pressed

LOGIC
Once you understand this then you can do it in any language.

Accept a number from the user of which the factorial you must obtain. Now use another variable with an initial value of 1 and store the value of the factorial in this variable. Start a loop from 1 to a so that each number gets multiplied by the product of the previous numbers which are stored in b. Since b= b * c;

Loop 1
c=1
b=1
b=b*c=1

Loop 2
c=2
b=1
b=b*c=2

Loop 3
c=3
b=2
b=b*c=6

and so on

Even I am in 11th man.

BTW printf is used in C not in C++.
 

Deus Ex Machina

Active Member
Adept
Mar 18, 2005
449
8
32
34
akshitmohan said:
Nice reply, but i didnt get one thing -

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

PLEASE REPLY ASAP.

That Didn't work ... ^^ :( ..
You mean this didn't work in TurboC ?!!!

Ok, I hope you have named the file as .c not as .cpp. TurboC handles both files differently based on file extension.

I tried it on gcc 3.2 and it works.

------------------------------------------

Ooops, I think you need the code in C++.

So make the following changes in my code ( only in main() function)

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

#include<iostream.h> //not stdio.h

int fact()

{

// usual code

}

int main()

{

cout<<fact(1); //displays factorial of 1

cout<<fact(5); //displays factorial of 5

return 1;

}

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

Forget about the stdio.h and printf() part. My bad.
 

kcab

Banned
Sep 15, 2006
63
1
0
@thunderbolt, you are making things too complex from class 11 point of view. Even I can hardly understand what you have written. Please use only basic codes like I have, we have just started C++, we are not mid way through as yet!
 

Keane 16

New Member
Disciple
Feb 7, 2006
2,173
16
0
28
if fact is a function itself, then why've you declared it with int ? :S ..

@ kcab -

#include <iostream.h>

#include <conio.h>

void main ()

{

clrscr();

int a,b,c;

cin>>a;

b=1

for(int c=1; c<=a; c++)

{

b=b*c

}

cout<<b;

getch();

}

^^ Tried this ... doesnt work .. sum prob with for loop line ..
 

Deus Ex Machina

Active Member
Adept
Mar 18, 2005
449
8
32
34
kcab said:
@thunderbolt, you are making things too complex from class 11 point of view. Even I can hardly understand what you have written. Please use only basic codes like I have, we have just started C++, we are not mid way through as yet!
Infact everyone has already given the "regular" way, I thought may be he would be able to appreciate a alternate approach.

akshitmohan said:
if fact is a function itself, then why've you declared it with int ? :S ..
"fact" is a function but it needs to return a integer value. Hence the return type is int.
But I guess this is too much for you. :) You should try the loop-approach that other ppl have given already. More suitable.
 

Keane 16

New Member
Disciple
Feb 7, 2006
2,173
16
0
28
#include <iostream.h>

#include <conio.h>

void main ()

{

clrscr();

int a,b,c;

cin>>a;

b=1

for(int c=1; c<=a; c++)

{

b=b*c

}

cout<<b;

getch();

}
Something wrong :( ..
 

kcab

Banned
Sep 15, 2006
63
1
0
int c;
us spaces eg: for ( c=1; c<5; c++)
{
}

try is this works

----------
don't do int c twice
 

Deus Ex Machina

Active Member
Adept
Mar 18, 2005
449
8
32
34
akshitmohan said:
It only works fine uptill 7. try 9 ... value goes -ve ..
Because you have declared b aas int. It has a upper limit of +32768. factor of 9 must be going above this.
Try declaring b as long.
 

Deus Ex Machina

Active Member
Adept
Mar 18, 2005
449
8
32
34
akshitmohan said:
Long, we haven't done. How about float ?
float is used to handle decimal point nos. Factorial of a no. never goes into decimal.
Or you can use unsigned int. it effectively doubles the range of positive nos. an int can accomodate.