To find the sum of digits

Here is the program to find the sum of the digits of the entered number. Main idea in this program is to slice down the given number into digits and to operate on them.

Logic : The program asks the user to enter the number, to find out the sum of its digits. Sets the flag ‘sum’ to zero, implies that sum till now is Nil. Stores the entered number as Num. The while loop slices the number into digits. In each iteration, the current digit gets added up to the flag ‘Sum’ . Finally it prints out the number.

The same logic can be implemented for other programs too, where the situation comes to slice up the given number, as in the case of  Reversing the number.

void main()
long int num,sum = 0,dig;
printf(“\n\n\t ENTER A NUMBER…: “);
dig = num % 10;
sum = sum + dig;
num = num / 10;
printf(“\n\t SUM OF DIGITS IS…: %ld”, sum);

Download exe and source code here.

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.