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