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; }
This is a discussion on Odd / Even numbers within the C++ Programming forums, part of the General Programming Boards category; I saw this on the forum about 3 weeks ago but can't seem to find it. All I want to ...
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; }
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
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....![]()
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
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![]()
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
Math Lesson of the day
what the cubed root of 512, no cheats!!!!
and only ahluka can answer.
128![]()
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
close its 8
8 * 8 * 8 = 512
Oh no, my 3rd U grade in Maths this year.
*points gun to head*
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
Code:if(number % 2) { /* number is odd */ } else { /* number is even */ }
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
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.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
>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.
My best code is written with the delete key.