I'm going to be honest this is a homework question but its one specific thing that I am confused with. All the parts of the problem have to be done with while loops. I have 2 different loops both of them work separately but when I run them together the odd numbers are displaying but it just says even total = 0. Should they not both be separate while loops?? I tried doing it as an if else but that was even worse than this and there will be other parts of the problem that need to manipulate the same original numbers.
#include <iostream>
usingnamespace std;
int main ()
{
int firstNum, secondNum;
int mod;
int even = 0;
int odd;
int counter;
cout << "Enter two numbers the first must be lower than the second" << endl;
cin >> firstNum >> secondNum;
mod = (firstNum % 2);
while (firstNum <= secondNum)
{
if (mod != 0)
cout << firstNum << " " << endl;
firstNum = firstNum + 2;
}
while (firstNum <= secondNum)
{
if (mod == 0);
even = firstNum + even;
firstNum = firstNum + 2;
}
cout << "even total = " << even << endl;
system ("pause");
return 0;
}
Shouldn't the mod ==/!= 0 conditions for your if statements be conditions for your while loops? The way it is now, the first while loop will increase firstNum until it's greater than secondNum, and then the second loop will never run, leaving even equal to zero.
Honestly, it's hard to help because without a description I can't actually pin down what it is this program is trying to do. I rewrote your first example as Albatross suggested, and it seems to work: if firstNum is even, the second loop runs. If it's odd, the first one runs - of course, even will still be 0 if firstNum is odd.
Actually that was very helpful. I need them both to run using the same set of numbers.
So no matter if the firstNum is odd or even I need to know all the odd numbers in the set and what the total of all the even numbers are.
I tried it this way but I think I'm just getting further from the solution. Back to the drawing board I go... haha
#include <iostream>
usingnamespace std;
int main ()
{
int firstNum, secondNum;
int mod;
int even = 0;
int odd;
int counter;
cout << "Enter two numbers the first must be lower than the second" << endl;
cin >> firstNum >> secondNum;
mod = (firstNum % 2);
counter = firstNum;
cout <<"These are the odd numbers: " << endl;
while (mod !=0 && counter <= secondNum)
{
cout << counter << " " << endl;
counter = counter + 2;
}
mod = (firstNum % 2);
counter = firstNum;
while (mod == 0 && counter <= secondNum)
{
even = counter + even;
counter = counter + 2;
}
cout << "even total = " << even << endl;
system ("pause");
return 0;
}
You're on the right track. The important thing to realize is that if firstNum % 2 dosen't return 0, then it will definitely return 1. Which means that if the number is even (mod == 0) then we just need to add 1 to it to make it odd. Likewise, when it's odd (mod == 1) we just need to subtract 1 to make it even. So then the code becomes:
#include <iostream>
usingnamespace std;
int main() {
int firstNum, secondNum, mod, even = 0, odd, tempNum;
cout << "Enter two numbers, the first must be lower than the second." << endl;
cin >> firstNum >> secondNum;
mod = firstNum % 2;
cout << "These are all of the odd numbers in the set: " << endl;
tempNum = firstNum;
if (mod == 0) { //this means to to get all the odd numbers, we'll need firstNum + 1 aswell.
tempNum += 1;
}
while (tempNum <= secondNum) {
cout << tempNum << " ";
tempNum += 2;
}
cout << endl;
cout << "Even total: ";
if (mod != 0) { //like with the odd numbers, I'll need to subtract 1 here
firstNum -= 1;
}
while (firstNum <= secondNum) {
even += firstNum;
firstNum += 2;
}
cout << even << endl;
}
I haven't tested this, mind you, so I could be wrong and/or there could be errors EVERYWHERE! But I think it should work ;)
Guess he could use two arrays or vectors to store all the even and odd numbers and then print them all out and the total. Though,I am probably over complicating it by bringing that up.
Thanks that did the trick! I ended up doing something slightly different because it worked better with the rest of the problem that I didn't post in here.
1 2 3 4
if (mod != 0) //makes sure loop control variable is odd
counter = firstNum;
else
counter = firstNum + 1;
1 2 3 4
if (mod == 0) //makes sure LCV is even
counter = firstNum;
else
counter = firstNum + 1;