You are using a version without Ads of this website. Please, consider donating:

### EVEN AND ODD

I'm new here sir. Can you help me with this program.
All Numbers are random.

This will be the output

Enter Number: 4
Enter Number: 3
Enter Number: 2
Enter Number: 1
Enter Number 6

The Sum of all even numbers is: 12
The Sum of all odd numbers is 4

Here's a hint:

 ``12345678`` ``````if(num % 2 == 0) { //number is even } else { //number is odd }``````

Members here are more inclined to help once you've showed some of the code you've already started (for the sake of not doing homework for you, but to help you along with it instead).
 ``123456789101112131415`` ``````int main() { int numbers[] = {4,3,2,1,6}; int sum_evens(0), sum_odds(0); for (int n = 0; n < sizeof(numbers)/sizeof(int); ++n) { if (numbers[n] %2) sum_odds += numbers[n]; else sum_evens += numbers[n]; } return 0; }``````
Line 8 needs to be
`if(numbers[n] % 2 == 0)`.
Other than that, looks good :)
Last edited on
Your assignment has undefined behaviour, because it is not clear when to stop to enter numbers.:)
Coz our teacher made a program and it stops. It has only 5 there. He used "do" there,

@ajh32

how to open that? it only displays press any key to continue.
Last edited on
Then, how to do that? You only put numbers in the program not in the output?

Re:
 Line 8 needs to be if(numbers[n] % 2 == 0). Other than that, looks good :)

If the number at index n of the array 'numbers' is odd then:
 `` `` ``if (numbers[n] %2)``

will be !0 and hence will assign the odd number to 'sum_odds'

Re:
 Your assignment has undefined behaviour, because it is not clear when to stop to enter numbers.:)

The for(...) loop will iterate for the number of ints in the array 'numbers', I didn't add any user input as it was just an example program.
Can you make the input for that? Thankss
 Can you make the input for that? Thankss

Come on, you have to do somthing yourself. You can either enter the input in the program argument list or you can user user input while the program is running. Maybe, you need to learn how to do this yourself!
I'll just try to study it first. Because I'm a beginner sir
ajh32 wrote:
If the number at index n of the array 'numbers' is odd then:
Ah, didn't see you adding the odds there. I was wrong :p
Can someone help me?
only if you can prove youve learned something and your not just tricking people into doing work for you
I'm new here sir. And I'm new in c++ so I want to learn about that. I cant even make it see this.

#include <iostream>
using namespace std;

int main ()
{
int num1;
int num2;
int num3;
int even;
int odd;

if (num1,num2,num3% 2 == 0)
cout << "Enter number: ";
cin >> num1;
cout << num1<<" is an even number"<<endl;
cout << "Enter number: ";
cin >> num2;
cout << num2<<" is an even number"<<endl;
cout << "Enter number: ";
cin >> num3;
cout << num3<<" is an even number"<<endl;

if (num1+num2+num3 % 2 == 0)
{
even = num1 + num2 + num3;
cout << "The sum of even: "<<even;
}
else (num1+num2+num3 % 2 == 0);
{
odd = num1 + num2 + num3;
cout << "The sum of odd: "<<odd;
}

return 0;
}

always fail
You need to test for each number being odd or even separately.

The line of code:
`if (num1,num2,num3 % 2 == 0)`

Doesn't make sense and non of those variables have been initialised, so they will all contain random data! Not good at all.
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970`` ``````// // oddeven.cpp // // take numeric input from user, store in vector, // stop adding numbers to array when non-numeric input // encountered. Add up all even and odd numbers, and // display the sum of each to the user. // #include #include #include #include using namespace std; // // Get the numbers from the user // and assign to array. Keeps getting // user input until user enters a non- // numeric value, then exit: // void get_numbers(vector& numbers) { while (true) { int number; string input; cout << "Enter Number: "; getline(cin,input); stringstream ss(input); if (ss >>number) numbers.push_back(number); else break; } } // // Calculates total sum of even numbers // and total sum of odd numbers in array, // and display to user: // void output_results(vector& numbers) { int sum_evens(0), sum_odds(0); for (int n = 0; n < numbers.size(); ++n) { if (numbers[n] %2) sum_odds += numbers[n]; else sum_evens += numbers[n]; } cout << "The sum of all even numbers is: " << sum_evens << endl; cout << "The sum of all odd numbers is: " << sum_odds << endl; } // // Entry point to program: // int main(int argc, const char * argv[]) { vector numbers; get_numbers(numbers); output_results(numbers); return 0; }``````