C++ - my loop keeps on adding up to 0
Posted
by
user1756913
on Stack Overflow
See other posts from Stack Overflow
or by user1756913
Published on 2012-10-19T22:51:55Z
Indexed on
2012/10/19
23:01 UTC
Read the original article
Hit count: 163
so far here's my code
#include <iostream>
using namespace std;
int main ()
{
int num1 = 0;
int num2 = 0;
int sum = 0;
for(num2 = num1; num1 <= num2; num1 +=2) sum += num1;
num1 = num1 / 2 == 0? num1 : num1 + 1;
num2 = num2 / 2 == 0? num2 : num2 - 1;
cout << "Enter the First Number:" << endl;
cin >> num1;
cout << "Enter the Second Number:" << endl;
cin >> num2;
cout << "Total Sum: " << sum << endl;
} //end for
but the sum keeps on adding up to 0 :/
here's the problem.
Create a program that displays the sum of the even numbers between and including two numbers entered by the user. In other words, if the user enters an even number, that number should be included in the sum. For example, if the user enters the integers 2 and 7, the sum is 12 (2 + 4 + 6). If the user enters the integers 2 and 8, the sum is 20 (2 + 4 + 6 + 8 ). Display an error message if the first integer entered by the user is greater than the second integer.
© Stack Overflow or respective owner