using factorial in c++

hye ..i wanna ask how to count factorial in visual studio c++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <stdlib.h>

void main()
{
    int a, i, factorial=1 ;
    printf("Masukkan nilai faktorial : ");
    scanf("%d",&a);
    for(i=a;i>1;i--)
	{
        factorial = factorial * i;
		printf("%d! =  %d\n",a,factorial);
    }
	system("pause");
}


this code works ..but i need an output like this
3!=3*2*1=6
4!=4*3*2*1=24
5!=5*4*3*2*1=120
can anyone help me ..im newbie ..really appreciate it
Last edited on
Do you mean that when the input is 5, then the output is
5!=5*4*3*2*1=120

You can break that line into three parts:
1. The prefix (5!=)is printed before the loop.
2. The body (5*4*3*...) adds up during the loop.
3. The suffix (=120\n) shows after the loop.
can you show me sample of that ..i mean the prefix printed before the loop ..u mean before the printf or after the printf?? i cant quite catch that ..
1
2
3
4
5
6
7
8
int a = 1;
int b;
int c = 6;
printf("%d", a); /*prefix*/
for ( b = 3; b < 5; ++b ) {
  printf("x%d", b); /*body*/
}
printf("x%d\n", c); /*suffix*/
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <stdlib.h>

void main()
{
    int a, i, factorial=1,b=1,c=6 ;
    printf("Masukkan nilai faktorial : ");
    scanf("%d",&a);
    for(i=a;i>1;--i)
	{
        factorial = factorial * i;
		printf("%d! = (%d*%d) = %d\n",a,b,i,factorial);
    }
	system("pause");
}


Masukkan nilai faktorial : 5
5! = (1*5) = 5
5! = (1*4) = 20
5! = (1*3) = 60
5! = (1*2) = 120
Press any key to continue . . .

this is what i got ..
i still cant understand where to change the prefix, body n suffix ..can u adjust to my code for that??
thanx for helping

oh and i change a little of the code from previous one
Last edited on
You still print everything on line 12 and on every iteration too. Why did you actually add stuff to line 12, rather than take out some?

I told you to print in pieces and my example has three separate calls to printf that together form one line to stdout. If you look carefully at my three fprint calls you should see that they are all different and print different things.

The random values that my example happens to use have nothing to do with your problem.
ok bro ..thanx for replying
Topic archived. No new replies allowed.