How to determine if the Values is even or odd

Write a program that will ask the user to input an integer value. Your program should then determine if the integer value is an odd or even number. Depending on whether the number is odd or even, output the number is odd/even, and either divide or multiply the input number as shown in the process statement below. Also, see the sample output below.

Input - One whole number.

Process - Determine if the number is odd or even. When the number is odd multiply it by 12. When the number is even divided the number by 2.
Output - The input, if the input is odd or even, and the input either divided by 2 or multiplied by 12 and shown in an equation. Try to match the text and spacing shown in the sample output exactly.

Programming Details:
Use an if else construct and the modulus operator to determine if the number is evenly divisible by 2 and output the correct information and equation. The program should work correctly with any whole number input.


It should read out

Please enter a number: 4
the number 4 is an even number.
4 / 2 = 2

Please enter a number: 11
The number 11 is an odd number.
11 * 12 = 132



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
  #include <iostream>
using namespace std;

int main ()
{

int x;

cout << " Please enter a number;" << endl;

cin >> x;

if  (x%2 == 0)
{
    cout << x << " is an odd number" << endl;
    cout << x * 12 = << endl;
}
else
{
 cout << x << " is an even number" << endl;
 cout << x / 2 = << endl;

}
 return 0;
}




what am not sure about is how do i ask the computer to determine if its a even or odd number?
Last edited on
closed account (48T7M4Gy)
Modular arithmetic (for integers)

x is even if x%2 == 0, it's odd if it isn't even :)
Last edited on
Kemort how long did it take you to full understand c++ ? if you don't mind me asking
closed account (48T7M4Gy)
@MrJ
I'll answer you indirectly. C++ is another computer language to write recipes for a machine to do your bidding - make it as simple or as complicated as you know/need/want.

If you surf your way around the tutorials (% is in there under modular arithmetic IIRC) here and get a good textbook/lectures etc and try things out yourself I reckon you can get a good handle on the basics - substantial and not just mickey mouse - in one or two semesters part time.

Try ideas out with simple programs and don't get bogged down for hours and hours. So feel free to come back with your attempts and questions. Somebody will always give you a sensible answer. Another tip: don't write more than a dozen lines without testing. The biggest mistake made here is ppl asking where the error is in 150 lines of their pride and joy they've been stressing out on for days and weeks and magically someone here is supposedly got a magic wand to solve it.

Yet another tip: be discriminating but make use of google (include C++ in the search text)

http://www.cplusplus.com/doc/tutorial/

Hope that helps/answers Cheers :)
A few ways to determine odd or even.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

void result( const char *text, bool isOdd ) { cout << text << ( isOdd ? "Odd" : "Even" ) << endl; }

int main()
{
   string s;
   int n;
   bool isOdd;

   cout << "Enter n: ";
   cin >> n;

   isOdd = ( n % 2 != 0 )     ;        result( "Modulo operation                ", isOdd );
   isOdd =   n % 2            ;        result( "Modulo operation (implied bool) ", isOdd );
   isOdd =   n & 1            ;        result( "Last bit                        ", isOdd );
   isOdd = 2 * ( n / 2 ) != n ;        result( "Divide-multiply                 ", isOdd );
      stringstream ss;   ss << n;   ss >> s;
   isOdd = ( s.find_last_of( "13579" ) == s.size() - 1 );
                                       result( "Last digit of string:           ", isOdd );
}
Last edited on
> isOdd = n & 1 ;

Technically broken for negative integers if the implementation uses ones’ complement representation of integral types.

The representations of integral types shall define values by use of a pure binary numeration system. [Example: this International Standard permits two’s complement, ones’ complement and signed magnitude representations for integral types. —end example ] - IS


Use the simple, transparent, easily understandable and equally efficient code suggested by kemort.
Ah, I wondered about the bitwise operation. I've only got a limited number of systems to try it on and it worked OK on those - however, I haven't tried different integer types (yet).

In operational code I would always use the first modulo operation, for reasons of simplicity and transparency. I was just exploring what other options might be available. I wouldn't dream of ever computing with the last one ... but it's probably what our brains actually do!
Computers that used ones’ complement representation of integral types (Sperry Univac, Unisys, CDC etc.) are more or less extinct.

However, ones’ complement representation of integers is still in wide use; for example in the checksum field of IP headers.
Point taken.

Does
isOdd = abs(n) & 1;
make it portable (and a bit ugly)?

(I stress - I wouldn't dream of actually using this in code, though I can't help feeling that some of the other regularly-used bitwise "short-cuts" must be vulnerable to failing in ones'-complement environments.)
Last edited on
isOdd = abs(n) & 1; is fine everywhere;
if the type of n is int, isOdd = n & 1U ; is also fine.
LastChance Thank you, however i am not understanding any of that code you just wrote. I don't think i have gotten that far into the chapter to beginning to understand what is wrote. I absolutely appreciate the example shown to me , so i have a better understanding on what to come
would i have to use another int to get my second cout statement or could i shortcut it with a different method?
BTW You all are awesome. I hope one day in the future i can be at everyone standard in coding and help other like myself in pursuing coding. Thank You
closed account (48T7M4Gy)
Since the program only asks for one number that's all you need. You only need a second int if you want to store the result. It would not be unusual to have the second int variable and you might call it 'answer' or 'result'. Then you would cout << answer;
Last edited on
closed account (48T7M4Gy)
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
  #include <iostream>
using namespace std;

int main ()
{

int x;

cout << " Please enter a number;" << endl;

cin >> x;

if  (x%2 == 0)
{
    cout << x << " is an even number" << endl;
    cout << x  / 2 << endl;
}
else
{
 cout << x << " is an odd number" << endl;
 cout << x * 12 << endl;

}
 return 0;
}
closed account (48T7M4Gy)
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
  #include <iostream>
using namespace std;

int main ()
{

int x = 0;
int answer = 0;

cout << " Please enter a number;" << endl;

cin >> x;

if  (x%2 == 0)
{
    answer = x/2;
    cout << x << " is an even number" << endl;
    cout << answer << endl;
}
else
{
 cout << x << " is an odd number" << endl;
  answer = x*12;
 cout << answer << endl;

}
 return 0;
}
MrJ, once you have the code working, reread the assignment and try to get the output to be exactly what the assignment asks for. I suspect the processor will grade by mechanically comparing your output to his when given a set of inputs.
Topic archived. No new replies allowed.