Fatorial from 1 to 20

Feb 24, 2013 at 4:52pm
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
for(a=1;a<=10;a++)
{
b=1;
for(c=1;c<=a;c++)
b=b*c;
cout<<"Factorial of "<<a<<" is "<<n<<endl;
}
getch();
}



This program will show the factorial of 1 to 20.
But I want to show the output Like
1!=1=1
2!=1*2=2
3!=1*2*3=6
4!=1*2*3*4=24
...............


How can I do this?


Feb 24, 2013 at 8:35pm
After making minor adjustments to fit my environment to you're code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
#include<conio.h>

using namespace std;

void main()
{
unsigned long int a,b,c;
for(a=1;a<=10;a++)
{
b=1;
for(c=1;c<=a;c++)
b=b*c;
cout<<"Factorial of "<<a<<" is "<<b<<endl;
}
getch();
}


It ends up displaying this...

Factorial of 1 is 1
Factorial of 2 is 2
Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120
Factorial of 6 is 720
Factorial of 7 is 5040
Factorial of 8 is 40320
Factorial of 9 is 362880
Factorial of 10 is 3628800


Is this not what you wanted?
Feb 24, 2013 at 9:00pm
Try to avoid void Main, compilers are starting to not support that anymore.
Feb 24, 2013 at 9:00pm
It just needs a few extra cout statements scattered throughout the code.

The only one which is slightly complicated is skipping the last "*", it needs an if (condition); everything else seems fairly straightforward.
Feb 24, 2013 at 11:38pm
I need the output Like

1!=1=1
2!=1*2=2
3!=1*2*3=6
4!=1*2*3*4=24
5!=1*2*3*4*5=120
6!=1*2*3*4*5*6=720
7!=1*2*3*4*5*6*7=5040
8!=1*2*3*4*5*6*7*8=40320
9!=1*2*3*4*5*6*7*8*9=362880
10!=1*2*3*4*5*6*7*8*9*10=3628800


Thanks for all to supporting me :-)
Feb 24, 2013 at 11:53pm
I understand the requirements, I took your original code posted at the top of this thread and tweaked it, to get that output, it's nearly there already.

Just experiment a little with inserting extra cout statements and see how close you get. When you really get stuck, post the latest version of the code.
Feb 25, 2013 at 2:56pm
Finally , I got the answer.

#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a,b,c;
cout<<"0!=0"<<endl<<"1!=1"<<endl;
for(a=2;a<=10;a++)
{
cout<<a<<"!=";
c=1;
for(b=1;b<=a;b++)
{
c=c*b;
if(b<a)
cout<<b<<"*";
else cout<<b;

}
cout<<"="<<c<<endl;
}
getch();
return 0;
}



Thanks a lot my friends.
Feb 25, 2013 at 3:38pm
I'm pleased that you got this to work. Yesterday I was concerned that it sounded like I was not very helpful, but my instinct was that it was better to learn by experimenting rather than simply reading someone else's answer.

As it turns out, your code looks quite similar to the version I came up with.
Last edited on Feb 25, 2013 at 3:38pm
Feb 25, 2013 at 3:45pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int factorial(int number) {
	int temp;

	if(number <= 1) return 1;

	temp = number * factorial(number - 1);
	return temp;
}

int main()
{
   for(int i = 1; i <= 20; i++)
   {
      std::cout << factorial(i) << std::endl;
   }
}
Feb 25, 2013 at 4:02pm
Now if the question had been "How do I implement the factorial function using recursion", that would have been a great answer.
Feb 25, 2013 at 4:11pm
Ah I see that now. OP's post wasn't clear. I thought he was explaining what a factorial was (in case someone didn't know?).
Topic archived. No new replies allowed.