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: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>
|