C Introduction

Control Statement

Functions

Array

Pointers

Structure and Union

No Examples found for this topic - CodeHelpPro

Data Files

No Examples found for this topic - CodeHelpPro

# Program to find sum of digits

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.

## 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

## 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

Login

0 Comments

Inline Feedbacks

View all comments