by Dinesh Thakur Category: Control Structures

A program to determine the sum of digits of a given non-negative integer number using a while loop is presented in Program. The program segment given below does the same thing using a do...while loop. 

sum = 0;

do {

sum += num % 10;  /* add LS digit to digit sum */

num /= 10;       /* remove LS digit from num */

while (num > 0);


The only problem with this implementation is that the statements within the loop will be executed at least once giving incorrect results for negative numbers. To ensure that the given number num is non-negative, we can declare it of type unsigned int.




#include <stdio.h>

void main()


    int num,sum; /* given numbers */


    printf("Enter integer numbers: ");

    scanf ("%d", &num);

    sum = 0;



         sum += num % 10;  /* add LS digit to digit sum */

         num /= 10;             /* remove LS digit from num */

     }    while (num > 0);


    printf ("The Sum is = %d \n",sum);




       C Program Sum of Digits of a Given Integer Number with do-while loop

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles