Easy learning with example program codes

C program to print sum of digits

The below program gives the sum of all the digits in a variable using loop. The C printf statement is used to output the text and sum value to the screen.


Till the given Number is greater than 0.

Remainder = Modulus of Number with 10
Total Sum = Adding Remainder into the Total Sum
Number = Dividing Number by 10

C program to print sum of digits

#include <stdio.h>
void main()
int x,y;
int sum = 0;
printf("Enter a number to add digits:");    
int num = x;
y = x % 10;    
sum = sum + y;    
x = x / 10;    
printf("Sum of digits of %d = %d", num,sum);    


Enter a number to add digits:123456
Sum of digits of 123456 = 21
Posted in C   

Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at


We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki