Thread: Help on Sum of Digits Entered

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    8

    Help on Sum of Digits Entered

    Hey again, I am new to C and trying to wrap my head around it. Just for clarification I am not looking for a full solution, just a shove in the right direction.

    In my book in chapter 1 there is an exercise as follows.
    If a five digit number is input through the keyboard, write a program to calculate the sum of its digits.
    Ok so pretty straight forward. So far in this chapter only variables and operators have been introduced, no decisional statements or loops have technically been learned yet even though I know of them.

    I have been playing with the modulus operator to try and solve the problem, but I am not sure how to approach the problem. Some suggestions would be great!

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Well the first thing I thought of was taking the number as a string (by using chars). But I'm not sure how to do it without using decision making. I'm not very good at math or algorithms...

    What did you try to do with the modulus operator?

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    8
    The book hinted to use the modulus operator, so I think that the idea behind that is to take the number, and the remainders hint at what the digits were? I don't know if I explained that clearly, not sure how to approach this.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    digit = n%10;
    n /= 10; /* the rest */
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Look at integer division as well.
    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.

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    8
    Thanks for the help guys this is what I came up with.

    Code:
    #include <stdio.h>
    
    int main()
    {
        int digits, sum;
    
        scanf("%d", &digits);
    
        sum = digits % 10;
        digits /= 10;
        sum += digits % 10;
        digits /= 10;
        sum += digits % 10;
        digits /= 10;
        sum += digits % 10;
        digits /= 10;
        sum += digits % 10;
    
        printf("%d", sum);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Writing a Sum of Digits program
    By toadkiwi in forum C Programming
    Replies: 10
    Last Post: 03-13-2008, 03:06 AM
  2. Minor Problem
    By stewie1986 in forum C Programming
    Replies: 6
    Last Post: 11-30-2007, 08:40 AM
  3. a sum equal to or in excess of 100
    By lyoncourt in forum C Programming
    Replies: 6
    Last Post: 10-07-2007, 05:43 PM
  4. The sum of squared digits
    By wiz23 in forum C++ Programming
    Replies: 4
    Last Post: 04-10-2005, 09:05 AM
  5. Replies: 7
    Last Post: 05-26-2003, 05:44 PM