7️⃣C Program to Find the Sum of Digits of a Number (With Program and Explanation)
๐ข C Program to Find the Sum of Digits of a Number
๐ Problem Statement
Write a C program to find the sum of digits of a given number.
Example
If the input number is 456,
then the sum of digits = 4 + 5 + 6 = 15
๐ก Concept Used
In C, we use:
-
% 10→ to extract the last digit of a number -
/ 10→ to remove the last digit
We repeat this process using a while loop until the number becomes 0.
๐ง Algorithm
-
Start
-
Read an integer number
-
Initialize
sum = 0 -
While the number is greater than 0
-
Find last digit using
num % 10 -
Add digit to sum
-
Remove last digit using
num / 10
-
-
Print the sum
-
End
๐ป C Program: Sum of Digits of a Number
▶️ Output Examples
✅ Example 1
Input:
Output:
Comments
Post a Comment