Factorial of a number

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++.
 
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.
 
@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!
 
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 ..
 
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.
 
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.
 
Back
Top