HELP ME! C++ EXAM

1) Make a program that will determine whether the given number is even or not.
Sample Output
Input a number: 2
2 is an even number
3 is not an even number

2)Make a program that will determine whether the given number is odd or not
Sample Output:
Input a number: 1
1 is an odd number
2 is not an odd number

This is too easy not to figure out by yourself. Get familiar with the modulus operator.
I guess you're making this thread while having the exam haha. Just modulo :)
Another approach would be to simply take advantage of the bitwise and (&). http://www.cplusplus.com/doc/tutorial/operators/

I would have to say this is probably the easiest exam you will ever get and is probably the exam after a week or two of the course :P Or your teacher is teaching very slow.
@giblit yeah yeah absolutely slow its already midterm now.. we already had this question less than a week
Oh and by the way the modulus operator is the same as c = round(a/b) * b - a where c = a % b

examples:
10
c = (10/2) * 2 - 10 = 5 * 2 - 10 = 10 - 10 = 0

11
c = (11/2) * 2 - 11 = 6 * 2 - 11 = 12 - 11 = 1 (5.5 got rounded to 6)

Though I guess another approach would be to see if when it divides by 2 it doesn't have a remainder. By not having a remainder I mean 2.0 instead of 2.1 or 5.0 instead of 5.5. I think either that method or checking the first bit with bitwise and (&) would be the most effective. Compared to brute force dividing by 2 as many times as possible(while > 1) or using modulus.
Last edited on
Topic archived. No new replies allowed.