How to calculate the sum of odd numbers in C++ language

Written by gordon ansell Google
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to calculate the sum of odd numbers in C++ language
C++ uses the modulus operator to test if a number is odd. (Medioimages/Photodisc/Photodisc/Getty Images)

Calculating the sum of odd numbers in C++ can be accomplished by looping through a set of numbers and seeing if there is a remainder after dividing each number by 2. If there is a remainder, the number is odd and can be added to a running total of odd numbers. C++ uses the modulus operator, which is the “%” character, to find the remainder when dividing two numbers.

Skill level:
Moderately Easy

Other People Are Reading

Things you need

  • C++ compiler

Show MoreHide

Instructions

  1. 1

    Set up a variable to contain the sum of odd numbers with the following statement:

    int oddSum = 0;

    The program will add every odd number to this variable.

  2. 2

    Create a loop to generate a sequence of numbers with the following statement:

    for (int i = 1; i < 1000; i++) {

    This will loop through the numbers 1 to 999, storing the number in the “i” variable each time. Any numbers can be used in place of 1 and 1000.

  3. 3

    Check whether a number is odd and, if so, add it to the sum:

    if (i%2 != 0) { oddSum = oddSum + i; }

    The “%” on the first line is the modulus operator, which returns the remainder of dividing two numbers. The program divides “i” from the loop of numbers by 2, which for even numbers will be 0 (because dividing any even number by 2 leaves no remainder). This program wants odd numbers, so it only adds the number to the sum if the result of the modulus test is not equal to 0, indicated by the “!=“ operator. If the number passes the test and is odd, it is added to the running total.

  4. 4

    Close the “for” loop by adding a "}" character to the end of the program. The sum of odd numbers will be in “oddSum” variable when the program is executed.

  5. 5

    Complete the program with a printf statement. The entire program would look like this:

    include <stdio.h>

    void main() { int oddSum = 0; for (int i = 1; i < 1000; i++) { if (i%2 != 0) { oddSum = oddSum + i; } } printf (“The sum of all odd numbers = %d\n", oddSum); }

    The program should output: "The sum of all odd numbers = 250000"

Tips and warnings

  • The above program demonstrates use of the modulus operator to test if a number is odd and any integers could be used with the “for” loop, but it could be more efficient if the loop always starts with an odd number. The “for” loop could then be written like this:
  • for (int i = 1; i < 1000; i = i + 2) {
  • oddSum = oddSum + i;
  • }
  • The loop starts with an odd number and increments the “i” variable by 2 each time, which will always give the next odd number. The modulus test does not therefore need to be performed and the number can just be added to the running total.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.