C Program for Sum the digits of a given number Last Updated : 04 Aug, 2022 Comments Improve Suggest changes Like Article Like Report Given a number, find sum of its digits.Example : Input : n = 687 Output : 21 Input : n = 12 Output : 3 1. Iterative: C // 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; } Time Complexity: O(log10n) Auxiliary Space: O(1) How to compute in single line? c # include<stdio.h> /* Function to get sum of digits */ int getSum(int n) { int sum; /* Single line that calculates sum */ for (sum=0; n > 0; sum+=n%10,n/=10); return sum; } int main() { int n = 687; printf(" %d ", getSum(n)); return 0; } 2. Recursive c int sumDigits(int no) { return no == 0 ? 0 : no%10 + sumDigits(no/10) ; } int main(void) { printf("%d", sumDigits(687)); return 0; } Please refer complete article on Program for Sum the digits of a given number for more details! Comment More infoAdvertise with us Next Article C Program for Sum the digits of a given number K kartik Follow Improve Article Tags : C Language Similar Reads Sum of Digits of a Number Given a number n, find the sum of its digits.Examples : Input: n = 687Output: 21Explanation: The sum of its digits are: 6 + 8 + 7 = 21Input: n = 12Output: 3Explanation: The sum of its digits are: 1 + 2 = 3Table of Content[Approach 1] Digit Extraction - O(log10n) Time and O(1) Space[Approach 2] Using 6 min read Find the sum of digits of a number at even and odd places Given a number N, the task is to find the sum of digits of a number at even and odd places. Examples: Input: N = 54873 Output: Sum odd = 16 Sum even = 11 Input: N = 457892 Output: Sum odd = 20 Sum even = 15 Approach: First, calculate the reverse of the given number.To the reverse number we apply mod 13 min read To find sum of two numbers without using any operator Write a program to find sum of positive integers without using any operator. Only use of printf() is allowed. No other library function can be used.Solution It's a trick question. We can use printf() to find sum of two numbers as printf() returns the number of characters printed. The width field in 9 min read C Program to Calculate Sum of Natural Numbers Here we will build a C program to calculate the sum of natural numbers using 4 different approaches i.e. Using while loopUsing for loopUsing recursionUsing Functions We will keep the same input in all the mentioned approaches and get an output accordingly. Input: n = 10 Output: 55 Explanation: The s 3 min read How to Write a Command Line Program in C? In C, we can provide arguments to a program while running it from the command line interface. These arguments are called command-line arguments. In this article, we will learn how to write a command line program in C. How to Write a Command Line Program in C? Command line arguments are passed to the 2 min read Like