Re: [Dev-C++] factorial code additional
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Jason H. <jas...@bt...> - 2001-10-21 23:24:34
|
As long as it works that's the main thing. You might want to test it =
with the value 1.. remember that the factorial of 1 is 1.
Jason.
----- Original Message -----=20
From: Daniel Glenfield=20
To: jas...@bt... ; dev...@li... =
Sent: Monday, October 22, 2001 12:16 AM
Subject: Re: [Dev-C++] factorial code additional
Here's what I ended up doing.
#include <stdio.h>
int main()
{
int a,b,c;
printf("Input number");
scanf("%d",&a); /* %d is for an int I think in C. &a assigns =
that value to a */
=20
c =3D a-1;
b =3D a*c;
c--;
while (c >=3D 1)=20
{
=20
=20
b =3D b*c--;
=20
=20
=20
}
printf ("The factoral of %d is %d\n", a, b );
return 0;
}
But thanks, I'll look over yours :)
>From: "Jason Hardman"=20
>To: "Daniel Glenfield"=20
>Subject: Re: [Dev-C++] factorial code additional=20
>Date: Mon, 22 Oct 2001 00:05:51 +0100=20
>=20
>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.=20
>=20
>#include=20
>#include=20
>=20
>inline int factorial(int n);=20
>=20
>int main(int argc, char *argv[])=20
>{=20
> int a, b;=20
> printf("Input number: ");=20
> scanf("%d", &a);=20
>=20
> // calculate factorial here=20
> b =3D factorial(a);=20
>=20
> printf("The factoral of %d is %d\n", a, b);=20
>=20
> system("PAUSE");=20
> return 0;=20
>}=20
>=20
>// factorial function=20
>inline int factorial(int n)=20
>{=20
> int r =3D 1;=20
> for(; n > 1; r *=3D n, --n);=20
> return r;=20
>}=20
>=20
>If you wanted to do the same with while loop then you could use:=20
>=20
>b =3D 1; // first initialise b=20
>while(a > 1)=20
>{=20
> b =3D b * a--; // or b *=3D a--;=20
>}=20
>=20
>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.=20
>=20
>Jason.=20
> ----- Original Message -----=20
> From: Daniel Glenfield=20
> To: dev...@li...=20
> Sent: Sunday, October 21, 2001 11:00 PM=20
> Subject: [Dev-C++] factorial code additional=20
>=20
>=20
> what is wrong?=20
> I want to get the factorial of any number inputted=20
> It must have three veriables btw=20
>=20
> #include=20
> int main()=20
> /* This is a program which calculates the factorial of a number */=20
> {=20
> int a,b;=20
> printf("Input number");=20
> scanf("%d",&a=20
> while (a >=3D 1)=20
> {=20
> a*(a--);=20
> b =3D a;=20
>=20
>=20
> }=20
> printf ("The factoral is %d\n", b );=20
> return 0;=20
> }=20
>=20
>=20
>=20
=
>------------------------------------------------------------------------=
------=20
> Get your FREE download of MSN Explorer at http://explorer.msn.com=20
> _______________________________________________ Dev-cpp-users =
mailing list Dev...@li... =
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users=20
-------------------------------------------------------------------------=
-----
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
|