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:
**


1 comment:

otim emmy said...

very helpful