little c++ question

Discussion in 'Web Design & Coding' started by mike09, Jun 7, 2003.

  1. mike09

    mike09 Moderator

    Messages:
    531
    Location:
    Washingtonville , New York
    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. NetRyder

    NetRyder Tech Junkie Folding Team

    Messages:
    13,256
    Location:
    New York City
    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. NetRyder

    NetRyder Tech Junkie Folding Team

    Messages:
    13,256
    Location:
    New York City
    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 :)
     
  4. mike09

    mike09 Moderator

    Messages:
    531
    Location:
    Washingtonville , New York
    so what would the header file be...
    modulus.h
    im not sure cause i havent learned it yet.
     
  5. NetRyder

    NetRyder Tech Junkie Folding Team

    Messages:
    13,256
    Location:
    New York City
    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. NetRyder

    NetRyder Tech Junkie Folding Team

    Messages:
    13,256
    Location:
    New York City
    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 :)
     
  7. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    I think stdio.h or if not that one then certainly math.h should have it
     
  8. mike09

    mike09 Moderator

    Messages:
    531
    Location:
    Washingtonville , New York
    thank you all very much for the help.
     
  9. NetRyder

    NetRyder Tech Junkie Folding Team

    Messages:
    13,256
    Location:
    New York City
    You're welcome :)
     
  10. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA

    Nope, both are not needed.
     
  11. NetRyder

    NetRyder Tech Junkie Folding Team

    Messages:
    13,256
    Location:
    New York City
    That's right...the math operators are just there for you to use :D
     
  12. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    Only the basic ones.