Write a C program to enter any number and calculate sum of digits of number using for loop. C program to find sum of digits of a number. How to find sum of digits of a number in C programming.

Input any number: 1234

Output sum of digits: 1 + 2 + 3 + 4 = 10

Happy coding ;)

**Example:**Input any number: 1234

Output sum of digits: 1 + 2 + 3 + 4 = 10

### Required knowledge:

Basic C programming, While loop### Logic to find sum of digits

Before moving on to the logic of finding sum of digits one thing you must be done with is how to find the last digit of any number in C. Now, moving on to logic of this program can be divided in three basic steps:- Find the last digit of number by performing modular division.
- Add the last digit just found above to sum.
- Remove the last digit from number by dividing the number by 10.

### Program to find sum of digits

/** * C program to find sum of its digits of a number */ #include <stdio.h> int main() { int num, sum=0; /* Reads a number from user */ printf("Enter any number to find sum of its digit: "); scanf("%d", &num); /* Repeat till num becomes 0*/ while(num!=0) { /* Find the last digit of num and add to sum */ sum += num % 10; /* Removes last digit from num */ num = num / 10; } printf("\nSum of digits = %d", sum); return 0; }

Output

Enter any number to find sum of its digit: 1234

Sum of digits = 10

Sum of digits = 10

Happy coding ;)

### You may also like

- Loop programming exercises index.
- C program to find product of digits of a number.
- C program to find sum of all even numbers between 1 to n.
- C program to find sum of all odd numbers between 1 to n.
- C program to find sum of all natural numbers between 1 to n.
- C program to find sum of first and last digits of a number.
- C program to find sum of all prime numbers between 1 to n.
- C program to find sum of all array elements.
- C program to find sum of two matrices.
- C program to print all factors of any number.
- C program to print reverse of a number.
- C program to check Prime numbers.
- C program to check Armstrong numbers.
- C program to print all Prime numbers between 1 to n.
- C program to print all Armstrong numbers between 1 to n.

< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).