In this example, I have shared a C program for Sum the digits of a given number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | // C program to compute sum of digits in number. # include<stdio.h> /* Function to get sum of digits */ int getSum(int n) { int sum = 0; while (n != 0) { sum = sum + n % 10; n = n/10; } return sum; } int main() { int n = 687; printf(" %d ", getSum(n)); return 0; } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.