I need to make a "for loop" that adds up only the odd numbers entered by the user. The even numbers would need to be ignored. The program would let the user only enter a certain number of entries, let’s say 4 entries. They would be prompted to enter a number and then enter one in. This would continue for each number until they reach 4 numbers entered. After the 4th is entered they would get the total for only the odd numbers that were entered. I know this seems simple but for some reason I can’t figure this out. Any help or suggestions is appreciated – Thanks.
If the trouble is determining whether a number is odd or even, the traditional answer is to use the % operator, which by default gives the remainder you'd get from doing a division. If a division by 2 would give a remainder of 0, it's even. If a division by 2 would give a remainder of 1, it's odd. Makes sense?
- Identify the variables
- Number the user needs to input
- The number of Entries
- The running total
- Ask the user to type the number of entries to add the numbers
- Create a for loop that will not loop more than the number of entries
- Inside the for loop
- Ask the user to enter an odd number
- Use the if statement to validate that the number is odd or even
- If the number is odd, then add it to the running total
- If the number is even, then let the user know the number is not odd and post-decrement the count of the loop
- Close the loop and notify the user of the total of the odd numbers addition
- If the number is even, then let the user know the number is not odd and post-decrement the count of the loop
I dont think decrementing is necessary. The OP says a certain number of numbers will be entered and the odds will be added while the evens will be ignored.
The loop will increment no matter the number is odd or even.
You brought a good point. I use the post-decrement to let the user to enter another value that is an odd number and does not count towards the number of entries. So it validates the input of the user. Nevertheless, I over-did it.
#include <iostream>
int main()
{
int number{ 0 };
unsignedint numberOfEntries{ 0 };
int total{ 0 };
std::cout << "Type the number of entries to add the odd numbers: ";
std::cin >> numberOfEntries;
for (unsignedint count = 0; count < numberOfEntries; count++)
{
std::cout << "Enter the odd number [" << count + 1 << "] to add: ";
std::cin >> number;
if (number % 2 != 0)
total += number;
elseif (number % 2 == 0)
{
std::cout << "The number " << number << " is even.\n";
count--;
}
}
std::cout << "The total is [" << total << "]\n";
return 0;
}
Thank you for all the helpful comments. I put what I have so far below. I got it to add up the separate input entries and total it after the 5th entry. Now I just need it to only add the odd numbers entered and ignore the even. Resulting in a total/sum of the odd numbers entered. I know %2 != 0 should look for the odd entries but I can’t seem to get it all to work?
#include <iostream>
using namespace std;
int main() {
int sum = 0;
int num[4];
for (int counter = 0; counter <= 4; counter++)
{
cout << "Enter a Number: ";
cin >> num[counter];
sum = sum + num[counter];
}
Below is an example of how to check the number for even or odd. This is to give you an idea so you can finish your code.
1 2 3 4 5 6 7
// To check if the number is even
if (num[counter] % 2 == 0)
cout << "The number " << num[counter] << " is even\n";
// To check if the number is odd
elseif (num[counter] % 2 != 0)
cout << "The number " << num[counter] << " is odd\n";
Thanks for the reply chicofeo. I did try adding that code but it still just adds up every number I enter in. I need to have it just add up the odd numbers. Below
is an example of entering in the numbers and how it is working now. As you can see it is adding up everything still.
Enter a Number: 10
Enter a Number: 33
Enter a Number: 17
Enter a Number: 41
Enter a Number: 66
sum: 167
Okay, I got everything running smoothly and needed to add one last thing. I thought it would be easy but its not working out that way. I need to use the continue statement for this. I have tried many variations but the program is still counting 7 when adding the sum.
#include <iostream>
using namespace std;
int main() {
int sum = 0;
int num[5];
for (int counter = 0; counter < 5; counter++){
if (counter == 7) {
continue;
}
cout << "Enter a Number: ";
cin >> num[counter];
if (num[counter] % 2 != 0)
sum = sum + num[counter];
}
Please use code tags when posting. Highlight your code and click the <> button to the right of the edit window.
Where you have if (counter == 7), the condition will never be true. Counter goes from 0 to 4 as controlled by the loop right above it.
Think of continue as saying "okay, I don't need to do anymore in this iteration of the loop." You want to use it if the number is even.
1 2 3 4 5 6 7
for (int counter = 0; counter < 5; counter++) {
....
if (num[counter] % 2 == 0) {
continue;
}
sum = sum + num[counter];
}
By the way, line 6 above can be shortened to sum += num[counter]; Also, notice that after you read a number and potentially add it to sum, you don't use it again. That means you don't actually need to keep it around. So instead of having an array of numbers, you could have a single int num and each time through the loop, overwrite it with the new number from the user.
What needs to happen is if the number 7 (which happens to be an odd number) is inputted (from keyboard). The program would then ignore it and not add it to the sum of all odd numbers entered. The continue statement also needs to be used.