C++ code to get sum of all odd numbers in given range






C++ code to get sum of  all odd  numbers in given range 

#include<iostream>

int main(){

    int number;
    int min,max;
    long sum =0;
 
    cout << "Enter the minimum range: ";
    cin >> min;

    cout << "Enter the maximum range: ";
    cin >> max;

    for(number = min;number <= max; number++)
         if(number % 2 !=0)
             sum = sum + number;

    cout << "Sum of odd numbers in given range is: " << sum;
 
    return 0;

}


C++ code to get sum of  all odd and even numbers in given range



#include<iostream>

int main(){

    int number;
    int min,max;
    long odd_sum =0,even_sum = 0;
 
    cout << "Enter the minimum range: ";
    cin >> min;

    cout << "Enter the maximum range: ";
    cin >> max;

    for(number = min;number <= max; number++)
         if(number % 2 != 0)
             odd_sum = odd_sum + number;
         else
             even_sum = even_sum + number;

   cout << "Sum of even numbers in given range is: " << even_sum << endl;
  cout << "Sum of odd numbers in given range is: " << odd_sum;
 
    return 0;

}


Programs of c++

Algorithm:
**


2 comments:

Otim Kilara said...

very helpful

TOM said...

This is so amazing that you have shared the C++ code to get a sum of all odd numbers in a given range. This will help me a lot. Thanks for sharing this with us. I really appreciate that. Now it's time to avail 1 litre bottle for more information.