Sum of Digits of a Number in C
Here, you will get and learn the program code to find Sum of Digits of a Number in C programming. This program is briefly explain with algorithm, DFD (Data Flow Diagram), and program code.
What is digit?
Digit is a numerical symbol used in mathematics and other disciplines such as computer science. Digits are used to represent numbers, values, and other quantities in various systems of notation.
Step 1. Input number from user.
Step 2. Get the remainder, quotient of number, by dividing 10.
Step 3. Add remainder in sum variable.
Step 4. Divide the number by 10, then replace number by quotient.
Step 5. Repeat Step 2,3,4 till then number is greater then 0.
Step 6. Print value of sum as sum of digits.
DFD (Data Flow Diagram)
Different 3 ways to find Sum of Digits of a Number in C
Method #1. Sum of Digits in C using while loop
Method #2. Sum of Digits in C using Functions
Method #3. Sum of Digits in C using Recursion
Sum of Digits in C using while loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | //Sum of Digits in C using while loop #include<stdio.h> int main() { int num, n , rem, sum = 0; printf("\nEnter a integer: "); scanf("%d", &num); n = num; while (num != 0) { rem = num % 10; sum += rem ; num = num / 10; } printf("\nSum of digit of %d is %d",n,sum); return 0; } |
Output
Check out our other C programming Examples