Show
Sum of digits C program to calculate the sum of digits of a number, we use modulus operator (%) to extract individual digits of a number and keep on adding them. Sum of digits of a number in C#include <stdio.h> int main() printf("Enter an integer\n"); t = n; while (t != 0) printf("Sum of digits of %d = %d\n", n, sum); return 0; If you wish, you can modify the input variable (n) and without using an additional variable (t), but we don't recommend it. Output of program: For example, if the input is 98, the variable sum is 0 initially98%10 = 8 (% is modulus operator, which gives us the remainder when 98 is divided by 10).sum = sum + remainderso sum = 8 now.98/10 = 9 because in C language, whenever we divide an integer by another one, we get an integer.9%10 = 9sum = 8 (previous value) + 9sum = 179/10 = 0. So finally, n = 0, the loop ends; we get the required sum. Download Add digits program. C program to find sum of digits using for loop#include <stdio.h> int main() printf("Enter a number\n"); for (scanf("%d", &n); n != 0; n = n/10) { printf("Sum of digits of a number = %d\n", sum); return 0; Calculate sum of digits in C without modulus operatorC program to find the sum of digit(s) of an integer that does not use modulus operator. Our program uses a character array (string) for storing an integer. We convert its every character into an integer and add all these integers. #include <stdio.h> int main() { int c, sum, t; char n[1000]; printf("Input an integer\n"); scanf("%s", n); sum = c = 0; while (n[c] != '\0') { t = n[c] - '0'; // Converting character to integer sum = sum + t; c++; } printf("Sum of digits of %s = %d\n", n, sum); return 0; } An advantage of this method is that the input integer can be huge, which we can't store in an int or a long long data type variable. See the example below. Output of program: Input an integer C program to find sum of digits of a number using recursion#include <stdio.h> int add_digits(int); int main() scanf("%d", &n); result = add_digits(n); printf("%d\n", result); return 0; int add_digits(int n) { return (n%10 + add_digits(n/10)); Improve Article Save Article Like Article Improve Article Save Article Given a number, find the sum of its digits. Examples :
Follow the below steps to solve the problem:
Below is the implementation of the above approach:
Time Complexity: O(log N) How to compute in a single line?The below function has three lines instead of one line, but it calculates the sum in one line using for loop. It can be made one-line function if we pass the pointer to the sum. Below is the implementation of the above approach:
Time Complexity: O(log N) Sum of the digits of a given number using recursion:Follow the below steps to solve the problem:
Below is the implementation of the above approach:
Time Complexity: O(log N) Sum of the digits of a given number with input as string:
Follow the below steps to solve the problem:
Below is the implementation of the above approach:
Time Complexity: O(log N) Sum of the digits of a given number using tail recursion:Follow the below steps to solve the problem:
Below is the implementation of the above approach:
OutputSum of digits is 15 Time Complexity: O(log N) Please write comments if you find the above codes/algorithms incorrect, or find better ways to solve the same problem. |