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