Thread: How to make program be able to tell whether a number is odd or even

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    11

    Exclamation How to make program be able to tell whether a number is odd or even

    I'm writing a program in C and have encountered an obstacle. Here's my pseudocode:

    prompt for 1st number
    read 1st number
    while EOF is not reached

    print the number
    if number is even

    print"This is an even number"

    else

    print"This is an odd number"

    read the next number

    I've got the basics down, but I can't figure out how to make the program figure out whether a number is odd or even. Any help would be appreciated.

    Colin

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    if(num%2 == 1) it's odd

  3. #3
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    the % is the modulus operator and returns wether or not there is a remainder after the number has been divided by 2 (an even number) - if there is a remainder it is odd
    Monday - what a way to spend a seventh of your life

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    A faster method than the one with modulo is:
    Code:
    if (number&1) puts("the number is odd");
       else puts("the number is even");
    klausi
    When I close my eyes nobody can see me...

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    11
    Thanks people! The answer was simple but I just couldn't see it because I was thinking about it too much.

    Colin

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help C program that reads Odd numbers and evens
    By Cyberman86 in forum C Programming
    Replies: 4
    Last Post: 02-27-2009, 11:59 AM
  2. Replies: 6
    Last Post: 02-19-2009, 07:19 PM
  3. Program to make stars mostly done, need help?
    By patso in forum C Programming
    Replies: 4
    Last Post: 04-07-2008, 10:45 PM
  4. want to make this small program...
    By psycho88 in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2005, 02:05 AM
  5. Replies: 3
    Last Post: 01-14-2003, 10:34 PM