Program to find sum of digits

0 Suresh Chand June 2, 2021

In this example, We will see the program to find sum of digits of a number. Here we will use while loop to find the sum of each digits of a number. To understand this example, You must have knowledge in following topics.

Download Application: Learn C Programming with Live Compiler

Algorithm

The algorithm of program to find sum of digits of a number is given below

Step 1: Start
Step 2: Initialize the variables m, sum, c and remainder
Step 3: Assign 0 to sum
Step 4: Take input from user and assign to n variable
Step 5: Repeat this step until n != 0
    Step 5.1: remainder = n % 10
    Step 5.2: sum = sum + remainder
    Step 5.3: n = n / 10
Step 6: Print sum
Step 7: End

Flowchart

The flowchart of program to find total sum of digits of a number is given below

Flowchart : Program to find sum of digits

CODE: Program to find sum of digits

Now, We will look on the code to check find the sum of digits of number.

#include<stdio.h>

int main(){

	int n, sum = 0, c, remainder;

    printf("Enter the number you want to add digits of:  ");
    scanf("%d", &n);

	while( n != 0 ) {
        remainder = n % 10;
        sum += remainder;
        n = n / 10;
    }

	printf("\nSum of the digits of the entered number is  =  %d", sum);

    return 0;
}

The output of above program is

Enter the number you want to add digits of:  123
Sum of the digits of the entered number is  =  6
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Join Our Newsletter Now

Get the latest tutorials, examples and projects updates.

Ads Block Detector Powered by codehelppro.com

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Disable Ad Block

Want Same System