While loop question!

This is a discussion on While loop question! within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main() float fact; /* what number do we want to calculate the ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    3

    While loop question!

    Code:
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    float fact;
    
    /* what number do we want to calculate the factorial of? */
    num = 10;
    
    /* Initialise the factorial */
    fact = 1.0;
    
    /* Count from 2 to num, multiply fact by counter each time */
    i = 2;
    while ( i <= num) {
       fact = fact *  i;
    i = i + 1;
    }
    
    /* Print the result */
    printf (“The factorial of &#37;d is %f\n”, num, fact);
    }
    The point of that was to calculate the factorial of 10, using a while loop, which worked fine, but I have this question that says Alter the program so that the counter counts from num down to 1, rather than the other way .

    I tried altering the code in the following way but it didn’t work:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    float fact;
    
    /* what number do we want to calculate the factorial of? */
    num = 10;
    
    /* Initialise the factorial */
    fact = 1.0;
    
    /* Count from 10 to num, multiply fact by counter each time */
    i = 10;
    while ( 0< i <= num) {
       fact = fact *  i;
    i = i - 1;
    }
    
    /* Print the result */
    printf (“The factorial of %d is %f\n”, num, fact);
    }
    Which should give me the same answer as the first code written but it doesn’t. Where have I gone wrong?

    Thanks!
    Last edited by hhhmortal; 11-27-2007 at 10:09 AM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Code:
    while ( 0< I <= num)
    This doesn't do what you think it does.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    int main() {
    Code:
    return 0;
    i is not declared.

    Is this the actual code that you compiled? If it is, what compiler are you using, and what is your warning level set to?

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    54
    You can use this for while
    Code:
    While(0<l && l<=num)
    but in your program l is undeclared . . . Check it out

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    3
    What do you mean i is not declared...?

    I used emacs as the editor.. and i've edited the code slightly maybe it was that..what you mean?

  6. #6
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    It means you're not telling the compiler what i is. The same goes for num. The compiler can't just guess "oh, i is an int, num is also an int" you have to declare to the compiler that they are.

    In code :
    Code:
    int i;
    int num;
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > While(0<l && l<=num)

    Capital i, or any case of L are terrible names for variables. It's also while() not While()

    0 < I <= num, is the same as, i E(0,num], in other words
    Code:
    if(i > 0 && i <= num)
        /* in range */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop question
    By JuzMe in forum C++ Programming
    Replies: 11
    Last Post: 04-20-2009, 08:39 AM
  2. Loop question
    By kwood965 in forum C Programming
    Replies: 6
    Last Post: 10-29-2008, 11:12 PM
  3. simple for loop function question
    By felicityxiv in forum C Programming
    Replies: 7
    Last Post: 05-06-2006, 11:43 PM
  4. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 12:08 PM
  5. while loop question
    By rayrayj52 in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2004, 05:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21