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

Thanks in Advance
Here's a hint:

1
2
3
4
5
6
7
8
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).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
closed account (Dy7SLyTq)
if your talking about op vlad, he said "this will be the output". ie thats the final product. if your talking about aj, your not supposed to enter numbers anywhere
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?
closed account (Dy7SLyTq)
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.
How about this then:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
// 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 <vector>
#include <string>
#include <iostream>
#include <sstream>

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<int>& 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<int>& 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<int> numbers;
    
    get_numbers(numbers);
    output_results(numbers);
    
    return 0;
}
closed account (Dy7SLyTq)
looks good
Topic archived. No new replies allowed.