-
Odd / Even numbers
I saw this on the forum about 3 weeks ago but can't seem to find it.
All I want to do is differentiate between an odd and even integer entered by the user:
Code:
int main ()
{
int number;
cout << "Number: ";
cin >> number;
// test etc
return 0;
}
-
You can use a modulus of 2 to check for a remainder, or do a bit-wise AND with 1.
gg
-
Thank you! Maths was never my strong point.... :(
-
Maybe you shouldn't be programming then.. programming involves a lot of math
in my school my Math teacher had a chart thing it had over 100 professions on it and lot of math things on the side, and if the profession required the math thing it had a dot..
only two that had the whole columns full were Programmer and physicist
-
Oh don't worry man I've been programming for 4 years and 5 months (not sure about the days ;)) and any maths problems I have I find enjoyable to solve by picking up a good maths book.
For some reason I find it hard to learn maths, not sure why. Programming and computing are my passions :)
-
Math Lesson of the day
what the cubed root of 512, no cheats!!!!
and only ahluka can answer.
-
-
close its 8
8 * 8 * 8 = 512
-
Oh no, my 3rd U grade in Maths this year.
*points gun to head*
-
Code:
if(number % 2) { /* number is odd */
}
else { /* number is even */
}
-
Cool programmers simply write:
Code:
bool is_odd(unsigned int x);
bool is_even(unsigned int x);
bool is_odd(unsigned int x)
{
if (x) return is_even(x - 1);
return false;
}
bool is_even(unsigned int x)
{
if (x) return is_odd(x - 1);
return true;
}
;-)
-
Yeah really cool. Superfast especially for big numbers. I hope cool was a synonym for dumb.
-
>programming involves a lot of math
It depends on the field. In my experience, the only math that is required is basic arithmetic. Higher math helps, but not for everyday programming.