while loop

This is a discussion on while loop within the C++ Programming forums, part of the General Programming Boards category; i am trying to write a while loop that computes the total of the odd numbers between 1 and 99 ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    17

    while loop

    i am trying to write a while loop that computes the total of the odd numbers between 1 and 99 inclusive. i wrote this but it just lists the numbers doesnt compute them. can somebody help?

    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main() {
    
    
       int x = -1;
       while ( x < 100 )
       {
               
    		
               x = x + 2;
    		   cout << x << endl;
       
       }
       
    
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by newbie101
    i am trying to write a while loop that computes the total of the odd numbers between 1 and 99 inclusive. i wrote this but it just lists the numbers doesnt compute them. can somebody help?
    Well, what in your code would make you think it would compute them?
    Code:
    while(x < 100) {
       x += 2;
       cout << x;
       total += x;
    }
    
    cout << total;
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    17
    i tried inputting this in the code but i keep getting errors. i declared total as an int but it still doesnt compute

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Code:
    total += x;
    This won't compute the number of odd numbers it will add them. To get the number of Odd Numbers Just increment total each time thrugh the loop.

    You could also do this without a loop.
    Code:
    Odds=N/2;
    Odds+=N%2;
    Last edited by Quantum1024; 05-03-2006 at 09:05 AM.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    17
    ok and how do i do that? i will continue to input stuff in the code to try and figure it out.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main() {
    
    
       int x = -1;
       int Total=0;
       while ( x < 100 )
       {
               
    		
               x = x + 2;
               Total++;
               cout << x << endl;
       
       }
       cout << Total << " Odd numbers" << endl;
    
    }

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Ah my bad, I took it to mean the sum. If you want a total iteratively (for whatever reason), just increment a counter each time you loop.

    Of course I don't see the point of this, since it's about the simplest thing to figure out arithmetically. If this was an assignment, tell your teacher it was a bad one.
    Last edited by SlyMaelstrom; 05-03-2006 at 09:10 AM.
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    17
    thanks i almost had it right before you replied. i gave total the value of 0 and i put in total++ but i forgot the ; after the total++. thanks for the help.

    now if i wanted to make this a for loop do i just change the file to for or is there more to it?

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Code:
    int x, total;
    for(x = 1, total = 1; x < 100; x += 2, total++)
       cout << x << '\n';
    
    cout << "Total: " << total << endl;
    Alot more compact, but I personally don't like multi-variable for loops, like this.
    Last edited by SlyMaelstrom; 05-03-2006 at 09:17 AM.
    Sent from my iPadŽ

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    17
    thanks got it, i was close but i didnt have the right syntax for the for loop. thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-19-2005, 11:30 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

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