hey can anyone help this is the code i have. i need to ouput the even and odd integers between two integers. i think what i have might work but when i run it it keeps running and never stops. also first num needs to be less then second anyone please help me

{ 
int firstNum, secondNum; 
int firstNum_1, secondNum_1;
int sumOdd, sumEven; 

sumOdd = 0; 
sumEven = 0; 
bool num0 = false;
	
	while (!num0)
	{
		cout << "Please enter in a number: ";
		cin >> firstNum;
		cout << "Plese enter a second number: ";
		cin >> secondNum;
		if (firstNum <= secondNum)
		{
			num0 = true;
		}
		else if (firstNum > secondNum)
		{
			cout << "First number should be less than Second number" << endl;
		}
	}
firstNum_1 = firstNum; 
secondNum_1 = secondNum; 
		 
while(firstNum != secondNum) 
{ 
	cout << "\n" << "Odd Numbers in between" << ' ' << firstNum << ' ' << "and" << ' ' << secondNum << ' ' << "is :" << ' ';
	if((firstNum % 2) != 0) 
	{ 
		cout << firstNum; 
		sumOdd += firstNum; 
	}
	if(((firstNum % 2) != 0) && (firstNum != secondNum)) 
	{ 
		cout << ","; 
	} 

while(firstNum_1 != secondNum_1) 
{ 
	cout << "\n" << "\n" << "Even Numbers in between" << ' ' << firstNum_1 << ' ' << "and" << ' ' << secondNum_1 << ' ' << "is :" << ' '; 
	if((firstNum_1 % 2) == 0) 
	{ 
		cout << firstNum_1; 
		sumEven += firstNum_1; 
	} 
	if(((firstNum_1 % 2) == 0) && (firstNum_1 != secondNum_1)) 
	{ 
		cout << ","; 
	} 
}
}

Dani AI

Generated

A short expert clarification and a compact fix.

The original runs forever because the loops that print odd/even never change the values used in their loop conditions and appear to be mis-braced (an inner loop is stuck). and pointed to the exit-condition problem; 's advice to use a for-loop avoids that class of mistake. A single pass across the range is clearer: test parity, print with commas, and accumulate sums. The example below validates the input (first <= second), includes both endpoints, and uses simple flags to format comma-separated output.

#include <iostream>
using namespace std;

int main() {
    int a, b;
    do {
        cout << "First number: "; if(!(cin >> a)) return 0;
        cout << "Second number: "; if(!(cin >> b)) return 0;
        if (a > b) cout << "First must be <= second. Try again.\n";
    } while (a > b);

    long long sumOdd = 0, sumEven = 0;
    bool firstOdd = true, firstEven = true;

    cout << "Odd numbers: ";
    for (int i = a; i <= b; ++i) {
        if (i % 2 != 0) {
            if (!firstOdd) cout << ", ";
            cout << i;
            sumOdd += i;
            firstOdd = false;
        }
    }
    cout << "\nSum of odd numbers: " << sumOdd << '\n';

    cout << "Even numbers: ";
    for (int i = a; i <= b; ++i) {
        if (i % 2 == 0) {
            if (!firstEven) cout << ", ";
            cout << i;
            sumEven += i;
            firstEven = false;
        }
    }
    cout << "\nSum of even numbers: " << sumEven << '\n';
    return 0;
}

Notes: to exclude endpoints iterate from a+1 to b-1; use long long for sums if ranges can be large; always ensure loop variables are advanced (or prefer for) and watch brace placement to avoid accidental nesting.

Recommended Answers

All 3 Replies

First of all please use code tags and proper indentation.

Check the exit criteria of the last two while loops.

Are you doing anything to modify the elements checked in the while condition?

Will the condition ever be false?

1. Use code tags
2. Looks like you never increase firstNum , so while(firstNum != secondNum) is always true.

Way way too complicated...

Input the two numbers like you did.
To print the even numbers, just use a for loop that goes from Number1 to Number2 incrementing i. If I%2....

Same with the odd numbers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.