• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

little c++ question

#1
im new to programming but how can i prove that am integer is even. cause i need ot make a program the says if the number that was inputed is either even or odd. thanks in advance.
 
#2
Use the modulus (%) operator. If the variable is "int foo" and you want to check that foo is even, here's the snippet of code:

Code:
if(foo%2 == 0) 
   cout << "The number is even \n";
else
   cout << "The number is odd \n";
 
#3
Just thought I'd mention what the modulus operator does, in case you don't already know.

When you divide two integers, the modulus is the remainder that you get. So for example, if you divide any even number by 2, you should get a remainder of 0. That's why you check to see if foo%2 == 0. If the value of foo was odd, the remainder would be 1.

Hope that helps :)
 
#5
No, you don't need any special headers to be included. It's a standard math operator just like +, -, * and /
Just include iostream.h for cout and cin.
 
#6
Code:
#include <iostream.h>

main() {
  int foo;
  cout << "Enter a number: ";
  cin >> foo;
  if(foo%2 == 0)
    cout << "The number is even \n";
  else
    cout << "The number is odd \n";

  return 0;
}
That's it :)
 

Members online

No members online now.

Latest posts

Latest profile posts

Hello, is there anybody in there? Just nod if you can hear me ...
Xie
What a long strange trip it's been. =)

Forum statistics

Threads
61,961
Messages
673,239
Members
89,015
Latest member
sanoravies