447,380 views
29 votes
29 votes
In c++ create a program that will allow the user to input a start and stop number, and print out the sum of the even or odd numbers between the 2 numbers.

User Kspearrin
by
2.1k points

1 Answer

21 votes
21 votes

Answer:

#include <iostream>
using namespace std;

int main() {

int start, stop, evenSum=0, oddSum=0;
cout << "Enter an integer to start: ";
cin >> start;

cout << "Enter an integer to stop: ";
cin >> stop;

for(int i = start; i <= stop; i++){
if (i % 2 == 0){
evenSum += i;
}
else{
oddSum += i;
}
}

cout << "Sum of odd numbers between " << start << " and " << stop << " is: " << oddSum<< endl;

cout << "Sum of even numbers between " << start << " and " << stop << " is: " << evenSum<< endl;

return 0;

}

Important: Please read explanation below

Step-by-step explanation:

My program computes the sums for odd and even numbers between start number and stop number inclusive.
For example if you enter start = 1 and stop = 8 it will compute odd sum as 1 + 3 + 5 + 7 = 16 and even sum as 2 + 4 + 8= 16

If you want to omit the start and stop numbers in the computation, change the for loop to the following:
for (int i = start + 1; i < stop; i++)

If you want to include the start but omit the end number,
for (int i = start; i < stop;i++)

Well, you get the drift

In c++ create a program that will allow the user to input a start and stop number-example-1
User Juergen
by
2.8k points