I'm not sure what you mean about it having 3 variables, but here is a =
version that works. Note that I have created a factorial function. I'm =
not entirly sure what you were doing in your version, but you forgot to =
store the variables you were working with, and didn't initialse b to =
anything. If you perform a calculation you have to assign it's result to =
something.
#include <cstdlib>
#include <cstdio>
inline int factorial(int n);
int main(int argc, char *argv[])
{
int a, b;
printf("Input number: ");
scanf("%d", &a);
=20
// calculate factorial here
b =3D factorial(a);
printf("The factoral of %d is %d\n", a, b);
=20
system("PAUSE");
return 0;
}
// factorial function
inline int factorial(int n)
{
int r =3D 1;
for(; n > 1; r *=3D n, --n);
return r;
}
If you wanted to do the same with while loop then you could use:
b =3D 1; // first initialise b
while(a > 1)=20
{
b =3D b * a--; // or b *=3D a--;
}
I hope from that you can figure out what you were doing wrong. The only =
reason to bring in a third variable would be to avoid changing 'a', and =
then you would just assign 'a' to a temporary variable i.e. 't' and then =
operate on it. Note that you don't need to multiply by 1 so it's 'a > 1' =
and not 'a >=3D 1', but it's a minor improvement.
Jason.
----- Original Message -----=20
From: Daniel Glenfield=20
To: dev...@li...=20
Sent: Sunday, October 21, 2001 11:00 PM
Subject: [Dev-C++] factorial code additional
what is wrong?
I want to get the factorial of any number inputted
It must have three veriables btw
#include <stdio.h>
int main()
/* This is a program which calculates the factorial of a number */
{
int a,b;
printf("Input number");
scanf("%d",&a
while (a >=3D 1)=20
{
a*(a--);
b =3D a;
=20
=20
}
printf ("The factoral is %d\n", b );
return 0;
}
-------------------------------------------------------------------------=
-----
Get your FREE download of MSN Explorer at http://explorer.msn.com
_______________________________________________ Dev-cpp-users mailing =
list Dev...@li... =
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|