Jun 8, 2016 at 4:44pm UTC
How do I write a sequence odd or even number program? I need to make one but I'm confused. Can somebody help me please, I would really appreaciate it! The program I have to make: for example i write a sequence of numbers: {2,−4,5,1,0,−4} the program displays: {2,2,1,1,2,2}. So even numbers as 2, odd numbers as 1 if u write multiple numbers.
Thanks!
Last edited on Jun 8, 2016 at 4:50pm UTC
Jun 8, 2016 at 4:53pm UTC
Hi sir,
could you show us what you did/tried?
PS: I love your work @ facebook :D
Jun 8, 2016 at 5:01pm UTC
The only program closest to this, which I was able to make is this:
But how do I make it able to make it change even and odd numbers to 2 and 1 with multiple numbers in sequence?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter an integer: " ;
cin >> n;
if ( n%2 == 0) {
cout << n << " 2." ;
}
else {
cout << n << " 1." ;
}
return 0;
}
Last edited on Jun 8, 2016 at 5:05pm UTC
Jun 8, 2016 at 9:45pm UTC
Last edited on Jun 8, 2016 at 9:46pm UTC
Jun 8, 2016 at 10:11pm UTC
You don't
have to use arrays.
The whole point is to be able to recognize whether a number is odd or even. This sounds like a job for... a
function!
1 2 3 4
bool is_even( int n )
{
return ...
}
Once you have that: for every number you input, check it against the function. If it is even, print a "2". If it is odd, print a "1".
Input may come directly from
cin
or some other file, or it may be from an array or some other sequence container (like a vector), etc.
Good luck.
Last edited on Jun 8, 2016 at 10:12pm UTC
Jun 8, 2016 at 10:15pm UTC
Sorry, my bad
You may use arrays
Jun 12, 2016 at 12:59am UTC
You need to include a while loop in you program. Check the tutorial on this site.