Thread: sum of digits in a number (while loop) please explain

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    48

    sum of digits in a number (while loop) please explain

    Hey all...also, i've started assembly on Intel 64 after dipping into C on FreeBSD for the past year. Would like to start using GDB.

    Code:
    // Program to give sum of digits of a number
    
    #include <stdio.h>
    
    int main(void)
    {
        long num, temp, digit, sum = 0;
    
        printf("Enter your number.\n");
        scanf("%ld", &num);
    
        temp = num;
    
        // num = 103
    
        while ( num > 0)
        {
            digit = num % 10;
            sum = sum + digit;
            num /= 10;
        }
    
        printf("Given number = %ld\n", temp);
        
        // output 103, 4
    
        printf("Sum of the digits %ld = %ld\n", temp, sum);
    }
    specifically, this while loop:

    Code:
        while ( num > 0)
        {
            digit = num % 10;
            sum = sum + digit;
            num /= 10;
        {
    Is this while loop looping num times until it reaches the value of greater than 0? Please expain.

    103 % 10 = 3 (digit)
    0 (sum) = 0 (sum) + 3
    103 (num) = 103 (num) / 10 (compound operator, division)

    Sincerely, lost

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,319
    Since you mentioned GDB, are you asking for an explanation of how you can use GDB to find the answer to your question?

    Note that your question was probably phrased wrongly: the while loop condition is num > 0, so the loop always loops once until it reaches the value of num greater than 0, otherwise it never loops at all and also never reaches the value of num greater than 0. Perhaps you want to ask if the loop loops num times until it reaches the value of num not greater than 0, but it is easy to see that this loop does not loop 103 times, which is why I'm wondering if your question is really about GDB.
    Last edited by laserlight; 1 Week Ago at 02:18 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    48
    I was just going on about my progress with C and I mentioned GDB. Please excuse the confusion.


    Was just looking or an explanation of the while loop line by line.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    3,523
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    48

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,532
    You may want to recheck this calculation...
    Code:
    103 (num) = 103 (num) / 10 (compound operator, division)
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting number of occurence of digits in a number
    By Azeem in forum C Programming
    Replies: 16
    Last Post: 10-12-2012, 11:46 PM
  2. Different digits in number
    By cowa in forum C Programming
    Replies: 2
    Last Post: 12-27-2009, 08:29 PM
  3. Number of digits in a decimal number
    By maverix in forum C Programming
    Replies: 7
    Last Post: 11-04-2007, 12:12 PM
  4. Number of digits?
    By falador in forum C Programming
    Replies: 2
    Last Post: 05-13-2004, 03:52 AM
  5. Odd/Even Digits in a Number-Help!
    By ProgrammingDlux in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2002, 10:39 PM

Tags for this Thread