Thread: Re: [Dev-C++] factorial code additional
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Daniel G. <sp...@ho...> - 2001-10-21 23:16:50
|
<html><div style='background-color:'><DIV>
<P>Here's what I ended up doing.</P>
<P> </P>
<P>#include <stdio.h></P>
<P>int main()</P>
<P>{<BR> int a,b,c;</P>
<P> printf("Input number");</P>
<P> scanf("%d",&a); /* %d is for an int I think in C. &a assigns that value to a */<BR> <BR> c = a-1;<BR> b = a*c;<BR> c--;<BR> while (c >= 1) </P>
<P> {<BR> <BR> <BR> b = b*c--;<BR> <BR> <BR> <BR> }<BR> printf ("The factoral of %d is %d\n", a, b );<BR> return 0;<BR>}</P>
<P><BR><BR><BR>But thanks, I'll look over yours :)</P></DIV>
<DIV></DIV>
<DIV></DIV>>From: "Jason Hardman" <JAS...@BT...>
<DIV></DIV>>To: "Daniel Glenfield" <SP...@HO...>
<DIV></DIV>>Subject: Re: [Dev-C++] factorial code additional
<DIV></DIV>>Date: Mon, 22 Oct 2001 00:05:51 +0100
<DIV></DIV>>
<DIV></DIV>>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.
<DIV></DIV>>
<DIV></DIV>>#include <CSTDLIB>
<DIV></DIV>>#include <CSTDIO>
<DIV></DIV>>
<DIV></DIV>>inline int factorial(int n);
<DIV></DIV>>
<DIV></DIV>>int main(int argc, char *argv[])
<DIV></DIV>>{
<DIV></DIV>> int a, b;
<DIV></DIV>> printf("Input number: ");
<DIV></DIV>> scanf("%d", &a);
<DIV></DIV>>
<DIV></DIV>> // calculate factorial here
<DIV></DIV>> b = factorial(a);
<DIV></DIV>>
<DIV></DIV>> printf("The factoral of %d is %d\n", a, b);
<DIV></DIV>>
<DIV></DIV>> system("PAUSE");
<DIV></DIV>> return 0;
<DIV></DIV>>}
<DIV></DIV>>
<DIV></DIV>>// factorial function
<DIV></DIV>>inline int factorial(int n)
<DIV></DIV>>{
<DIV></DIV>> int r = 1;
<DIV></DIV>> for(; n > 1; r *= n, --n);
<DIV></DIV>> return r;
<DIV></DIV>>}
<DIV></DIV>>
<DIV></DIV>>If you wanted to do the same with while loop then you could use:
<DIV></DIV>>
<DIV></DIV>>b = 1; // first initialise b
<DIV></DIV>>while(a > 1)
<DIV></DIV>>{
<DIV></DIV>> b = b * a--; // or b *= a--;
<DIV></DIV>>}
<DIV></DIV>>
<DIV></DIV>>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 >= 1', but it's a minor improvement.
<DIV></DIV>>
<DIV></DIV>>Jason.
<DIV></DIV>> ----- Original Message -----
<DIV></DIV>> From: Daniel Glenfield
<DIV></DIV>> To: dev...@li...
<DIV></DIV>> Sent: Sunday, October 21, 2001 11:00 PM
<DIV></DIV>> Subject: [Dev-C++] factorial code additional
<DIV></DIV>>
<DIV></DIV>>
<DIV></DIV>> what is wrong?
<DIV></DIV>> I want to get the factorial of any number inputted
<DIV></DIV>> It must have three veriables btw
<DIV></DIV>>
<DIV></DIV>> #include <STDIO.H>
<DIV></DIV>> int main()
<DIV></DIV>> /* This is a program which calculates the factorial of a number */
<DIV></DIV>> {
<DIV></DIV>> int a,b;
<DIV></DIV>> printf("Input number");
<DIV></DIV>> scanf("%d",&a
<DIV></DIV>> while (a >= 1)
<DIV></DIV>> {
<DIV></DIV>> a*(a--);
<DIV></DIV>> b = a;
<DIV></DIV>>
<DIV></DIV>>
<DIV></DIV>> }
<DIV></DIV>> printf ("The factoral is %d\n", b );
<DIV></DIV>> return 0;
<DIV></DIV>> }
<DIV></DIV>>
<DIV></DIV>>
<DIV></DIV>>
<DIV></DIV>>------------------------------------------------------------------------------
<DIV></DIV>> Get your FREE download of MSN Explorer at http://explorer.msn.com
<DIV></DIV>> _______________________________________________ Dev-cpp-users mailing list Dev...@li... https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
<DIV></DIV></div><br clear=all><hr>Get your FREE download of MSN Explorer at <a href='http://go.msn.com/bql/hmtag_itl_EN.asp'>http://explorer.msn.com</a><br></html>
|
|
From: Daniel G. <sp...@ho...> - 2001-10-21 23:37:53
|
<html><div style='background-color:'><DIV>
<P>phew thanks mate!!! code now reads</P>
<P>#include <stdio.h></P>
<P>int main()</P>
<P>{<BR> int a,b,c;</P>
<P> printf("Input number");</P>
<P> scanf("%d",&a); /* %d is for an int I think in C. &a assigns that value to a */<BR> <BR> c = a-1;<BR> <BR> b = 1;<BR> while (c >= 1) </P>
<P> {<BR> <BR> <BR> b = b*a--;<BR> c--;<BR> <BR> <BR> <BR> }<BR> printf ("The factoral is %d\n", b );<BR> return 0;<BR>}</P>
<P><BR>Thanks so much!!!!!!!!!!!!!!!!<BR><BR></P></DIV>
<DIV></DIV>
<DIV></DIV>>From: "Jason Hardman" <JAS...@BT...>
<DIV></DIV>>To: "Dev-C++" <DEV...@LI...>, "Daniel Glenfield" <SP...@HO...>
<DIV></DIV>>Subject: Re: [Dev-C++] factorial code additional
<DIV></DIV>>Date: Mon, 22 Oct 2001 00:24:22 +0100
<DIV></DIV>>
<DIV></DIV>>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.
<DIV></DIV>>
<DIV></DIV>>Jason.
<DIV></DIV>> ----- Original Message -----
<DIV></DIV>> From: Daniel Glenfield
<DIV></DIV>> To: jas...@bt... ; dev...@li...
<DIV></DIV>> Sent: Monday, October 22, 2001 12:16 AM
<DIV></DIV>> Subject: Re: [Dev-C++] factorial code additional
<DIV></DIV>>
<DIV></DIV>>
<DIV></DIV>> Here's what I ended up doing.
<DIV></DIV>>
<DIV></DIV>>
<DIV></DIV>>
<DIV></DIV>> #include <STDIO.H>
<DIV></DIV>>
<DIV></DIV>> int main()
<DIV></DIV>>
<DIV></DIV>> {
<DIV></DIV>> int a,b,c;
<DIV></DIV>>
<DIV></DIV>> printf("Input number");
<DIV></DIV>>
<DIV></DIV>> scanf("%d",&a); /* %d is for an int I think in C. &a assigns that value to a */
<DIV></DIV>>
<DIV></DIV>> c = a-1;
<DIV></DIV>> b = a*c;
<DIV></DIV>> c--;
<DIV></DIV>> while (c >= 1)
<DIV></DIV>>
<DIV></DIV>> {
<DIV></DIV>>
<DIV></DIV>>
<DIV></DIV>> b = b*c--;
<DIV></DIV>>
<DIV></DIV>>
<DIV></DIV>>
<DIV></DIV>> }
<DIV></DIV>> printf ("The factoral of %d is %d\n", a, b );
<DIV></DIV>> return 0;
<DIV></DIV>> }
<DIV></DIV>>
<DIV></DIV>>
<DIV></DIV>>
<DIV></DIV>>
<DIV></DIV>> But thanks, I'll look over yours :)
<DIV></DIV>>
<DIV></DIV>> >From: "Jason Hardman"
<DIV></DIV>> >To: "Daniel Glenfield"
<DIV></DIV>> >Subject: Re: [Dev-C++] factorial code additional
<DIV></DIV>> >Date: Mon, 22 Oct 2001 00:05:51 +0100
<DIV></DIV>> >
<DIV></DIV>> >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.
<DIV></DIV>> >
<DIV></DIV>> >#include
<DIV></DIV>> >#include
<DIV></DIV>> >
<DIV></DIV>> >inline int factorial(int n);
<DIV></DIV>> >
<DIV></DIV>> >int main(int argc, char *argv[])
<DIV></DIV>> >{
<DIV></DIV>> > int a, b;
<DIV></DIV>> > printf("Input number: ");
<DIV></DIV>> > scanf("%d", &a);
<DIV></DIV>> >
<DIV></DIV>> > // calculate factorial here
<DIV></DIV>> > b = factorial(a);
<DIV></DIV>> >
<DIV></DIV>> > printf("The factoral of %d is %d\n", a, b);
<DIV></DIV>> >
<DIV></DIV>> > system("PAUSE");
<DIV></DIV>> > return 0;
<DIV></DIV>> >}
<DIV></DIV>> >
<DIV></DIV>> >// factorial function
<DIV></DIV>> >inline int factorial(int n)
<DIV></DIV>> >{
<DIV></DIV>> > int r = 1;
<DIV></DIV>> > for(; n > 1; r *= n, --n);
<DIV></DIV>> > return r;
<DIV></DIV>> >}
<DIV></DIV>> >
<DIV></DIV>> >If you wanted to do the same with while loop then you could use:
<DIV></DIV>> >
<DIV></DIV>> >b = 1; // first initialise b
<DIV></DIV>> >while(a > 1)
<DIV></DIV>> >{
<DIV></DIV>> > b = b * a--; // or b *= a--;
<DIV></DIV>> >}
<DIV></DIV>> >
<DIV></DIV>> >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 >= 1', but it's a minor improvement.
<DIV></DIV>> >
<DIV></DIV>> >Jason.
<DIV></DIV>> > ----- Original Message -----
<DIV></DIV>> > From: Daniel Glenfield
<DIV></DIV>> > To: dev...@li...
<DIV></DIV>> > Sent: Sunday, October 21, 2001 11:00 PM
<DIV></DIV>> > Subject: [Dev-C++] factorial code additional
<DIV></DIV>> >
<DIV></DIV>> >
<DIV></DIV>> > what is wrong?
<DIV></DIV>> > I want to get the factorial of any number inputted
<DIV></DIV>> > It must have three veriables btw
<DIV></DIV>> >
<DIV></DIV>> > #include
<DIV></DIV>> > int main()
<DIV></DIV>> > /* This is a program which calculates the factorial of a number */
<DIV></DIV>> > {
<DIV></DIV>> > int a,b;
<DIV></DIV>> > printf("Input number");
<DIV></DIV>> > scanf("%d",&a
<DIV></DIV>> > while (a >= 1)
<DIV></DIV>> > {
<DIV></DIV>> > a*(a--);
<DIV></DIV>> > b = a;
<DIV></DIV>> >
<DIV></DIV>> >
<DIV></DIV>> > }
<DIV></DIV>> > printf ("The factoral is %d\n", b );
<DIV></DIV>> > return 0;
<DIV></DIV>> > }
<DIV></DIV>> >
<DIV></DIV>> >
<DIV></DIV>> >
<DIV></DIV>> >------------------------------------------------------------------------------
<DIV></DIV>> > Get your FREE download of MSN Explorer at http://explorer.msn.com
<DIV></DIV>> > _______________________________________________ Dev-cpp-users mailing list Dev...@li... https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
<DIV></DIV>>
<DIV></DIV>>
<DIV></DIV>>------------------------------------------------------------------------------
<DIV></DIV>> Get your FREE download of MSN Explorer at http://explorer.msn.com
<DIV></DIV>> _______________________________________________ Dev-cpp-users mailing list Dev...@li... https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
<DIV></DIV></div><br clear=all><hr>Get your FREE download of MSN Explorer at <a href='http://go.msn.com/bql/hmtag_itl_EN.asp'>http://explorer.msn.com</a><br></html>
|
|
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
|