Thread: C Programming Need Help with While Loop Sum of Odd numbers

  1. #1
    Registered User
    Join Date
    Jul 2020
    Posts
    1

    C Programming Need Help with While Loop Sum of Odd numbers

    Hi, I am currently working on an assignment where I am suppose to be creating a loop that outputs the sum of all odd numbers between 20 and 100. I thought my coding was right however the program is displaying the answer is 2480 instead of the correct answer of 2400. My code is posted below: Thanks!
    ---------------------------------------------------------------------------
    insert
    Code:
    //Declared variables for program and initializing
    int odd = 19, number = 2, result = 0, result2 = 0, totalOdd = 0;
    
    do{
       
        result = odd + number;
    
    
        //The first result adds 19 and 2 in which equals to 21
        result2 = result + number;
    
    
        //The second result adds 21 + 2, which equals 23, and 2 is incremented by 1
        number++;
        totalOdd += result2; //Calculates the total sum of the odd numbers
    
    
    }while (result2 > 20 && result2 < 100);//Condition is set for result2 to be greater than 20, but less than 100
        printf("The sum of the odd numbers between 20 and 100 is %i\n", totalOdd);
        //Outputs the total sum

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,874
    Wow, that is hard to follow code for a newbie to write.
    I take it you are not allowed to use a for loop.
    I am surprised the answer is so close to being correct.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    149
    Quote Originally Posted by MackAttack View Post
    Hi, I am currently working on an assignment where I am suppose to be creating a loop that outputs the sum of all odd numbers between 20 and 100. I thought my coding was right however the program is displaying the answer is 2480 instead of the correct answer of 2400. My code is posted below: Thanks!
    ---------------------------------------------------------------------------
    insert
    Code:
    //Declared variables for program and initializing
    int odd = 19, number = 2, result = 0, result2 = 0, totalOdd = 0;
    
    do{
       
        result = odd + number;
    
    
        //The first result adds 19 and 2 in which equals to 21
        result2 = result + number;
    
    
        //The second result adds 21 + 2, which equals 23, and 2 is incremented by 1
        number++;
        totalOdd += result2; //Calculates the total sum of the odd numbers
    
    
    }while (result2 > 20 && result2 < 100);//Condition is set for result2 to be greater than 20, but less than 100
        printf("The sum of the odd numbers between 20 and 100 is %i\n", totalOdd);
        //Outputs the total sum
    Code:
    int isOdd(int value)
    {
     return value % 2 == 0;
    }
    Now loop through your numbers and increment a counter if the test passes.
    The power of null.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,863
    Quote Originally Posted by Sir Galahad View Post
    Code:
    int isOdd(int value)
    {
     return value % 2 == 0;
    }
    Now loop through your numbers and increment a counter if the test passes.
    Assuming that stahta01 is right to guess that this assignment stipulates the use of a do while loop rather than a more natural for loop or computing with a closed form expression, I think it'll be simpler to just keep odd and totalOdd, start odd at 21 then construct the loop condition with += to get the next odd number while checking to see if that next odd number is less than 100.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    698
    Odd numbers between 20 and 100 are between 21 (not 19) and 99:

    Code:
    #include <stdio.h>
    
    int main( void )
    {
      int n = 21;   // first odd value between 20 and 100.
      int sum = 0;
    
      do 
      {
        sum += n;
        n += 2;     // next odd value.
      } while ( n < 100 );
    
      printf( "%d\n", sum );
    }

  6. #6
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    149
    Quote Originally Posted by flp1969 View Post
    Odd numbers between 20 and 100 are between 21 (not 19) and 99:

    Code:
    #include <stdio.h>
    
    int main( void )
    {
      int n = 21;   // first odd value between 20 and 100.
      int sum = 0;
    
      do 
      {
        sum += n;
        n += 2;     // next odd value.
      } while ( n < 100 );
    
      printf( "%d\n", sum );
    }
    It's nice to be helpful, but remember, this is a homework assignment...
    The power of null.

  7. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    698
    Quote Originally Posted by Sir Galahad View Post
    It's nice to be helpful, but remember, this is a homework assignment...
    Yep, I notice... Since did a good try I thought it wasn't that harmul to show a different approach...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Averaging two different numbers/loop?
    By Xeydrian in forum C Programming
    Replies: 6
    Last Post: 10-08-2014, 04:11 PM
  2. How to for() loop with hex numbers?
    By monk64 in forum C Programming
    Replies: 5
    Last Post: 04-08-2010, 11:25 PM
  3. HEX-numbers after incrementation x in for-loop.
    By Jelte in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2009, 10:36 AM
  4. while loop and numbers
    By rahulsk1947 in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 05:59 AM

Tags for this Thread