Thread: I need a little help starting this...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    1

    I need a little help starting this...

    I've just recently started programming with C at college and I've been given a problem that has me stumped.

    6. Write a program to determine whether the sum of digits of a three digit input number is even or odd. The input may have less than 3 digits, but never more. For instance, the sum of digits of 145 is 1+4+5. The answer is 10 which is even. Your program should first sum the digits and then determine whether this sum is even or odd. USE A LOOP STRUCTURE TO SEPARATE THE DIGITS AND ADD THEM.

    How would I even go about starting this problem? Thanks for any help.

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Any 3 digit number can be written as :

    a*10^2 + b*10^1 + c*10^0

    Let's call this number m. Now notice that :

    m mod 10 = c
    m mod 100 = b*10 + c
    m mod 1000 = c*100 + b*10 + c

    Noticing a pattern yet ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Discover what 145 % 10 does, and what 145 / 10 does.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Starting programs
    By Molokai in forum C Programming
    Replies: 1
    Last Post: 04-16-2009, 10:10 AM
  2. Replies: 9
    Last Post: 03-20-2009, 05:22 PM
  3. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  4. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM
  5. question about reading inputs starting with 0
    By jibbles in forum C Programming
    Replies: 8
    Last Post: 08-09-2004, 03:27 AM