Finding sum of digits of a number until sum becomes single digit
Last Updated :
15 Feb, 2025
Given an integer n, we need to repeatedly find the sum of its digits until the result becomes a single-digit number.
Examples:
Input: n = 1234
Output: 1
Explanation:
Step 1: 1 + 2 + 3 + 4 = 10
Step 2: 1 + 0 = 1
Input: n = 5674
Output: 4
Explanation:
Step 1: 5 + 6 + 7 + 4 = 22
Step 2: 2 + 2 = 4
[Naive Approach] By Repetitively Adding Digits
The approach is focused on calculating the digital root of a number, which is the result of summing the digits repeatedly until a single-digit value is obtained. Here's how it works conceptually:
- Sum the digits: Start by adding all the digits of the given number.
- Check the result: If the sum is a single-digit number (i.e., less than 10), stop and return it.
- Repeat the process: If the sum is still more than a single digit, repeat the process with the sum of digits. This continues until a single-digit sum is reached.
C++
// C++ program to find the digit sum by
// repetitively Adding its digits
#include <iostream>
using namespace std;
int singleDigit(int n) {
int sum = 0;
// Repetitively calculate sum until
// it becomes single digit
while (n > 0 || sum > 9) {
// If n becomes 0, reset it to sum
// and start a new iteration.
if (n == 0) {
n = sum;
sum = 0;
}
sum += n % 10;
n /= 10;
}
return sum;
}
int main() {
int n = 1234;
cout << singleDigit(n);
return 0;
}
C
// C program to find the digit sum by
// repetitively Adding its digits
#include <stdio.h>
int singleDigit(int n) {
int sum = 0;
// Repetitively calculate sum until
// it becomes single digit
while (n > 0 || sum > 9) {
// If n becomes 0, reset it to sum
// and start a new iteration.
if (n == 0) {
n = sum;
sum = 0;
}
sum += n % 10;
n /= 10;
}
return sum;
}
int main() {
int n = 1234;
printf("%d", singleDigit(n));
return 0;
}
Java
// Java program to find the digit sum by
// repetitively Adding its digits
class GfG {
static int singleDigit(int n) {
int sum = 0;
// Repetitively calculate sum until
// it becomes single digit
while (n > 0 || sum > 9) {
// If n becomes 0, reset it to sum
// and start a new iteration.
if (n == 0) {
n = sum;
sum = 0;
}
sum += n % 10;
n /= 10;
}
return sum;
}
public static void main(String[] args) {
int n = 1234;
System.out.println(singleDigit(n));
}
}
Python
# Python program to find the digit sum by
# repetitively Adding its digits
def singleDigit(n):
sum = 0
# Repetitively calculate sum until
# it becomes single digit
while n > 0 or sum > 9:
# If n becomes 0, reset it to sum
# and start a new iteration
if n == 0:
n = sum
sum = 0
sum += n % 10
n //= 10
return sum
if __name__ == "__main__":
n = 1234
print(singleDigit(n))
C#
// C# program to find the digit sum by
// repetitively Adding its digits
using System;
class GfG {
static int singleDigit(int n) {
int sum = 0;
// Repetitively calculate sum until
// it becomes single digit
while (n > 0 || sum > 9) {
// If n becomes 0, reset it to sum
// and start a new iteration.
if (n == 0) {
n = sum;
sum = 0;
}
sum += n % 10;
n /= 10;
}
return sum;
}
static void Main() {
int n = 1234;
Console.WriteLine(singleDigit(n));
}
}
JavaScript
// JavaScript program to find the digit sum by
// repetitively Adding its digits
function singleDigit(n) {
let sum = 0;
// Repetitively calculate sum until
// it becomes single digit
while (n > 0 || sum > 9) {
// If n becomes 0, reset it to sum
// and start a new iteration.
if (n === 0) {
n = sum;
sum = 0;
}
sum += n % 10;
n = Math.floor(n / 10);
}
return sum;
}
// Driver Code
const n = 1234;
console.log(singleDigit(n));
Time Complexity: O(log10n), as we are iterating over the digits of the number.
Auxiliary Space: O(1)
We know that every number in the decimal system can be expressed as a sum of its digits multiplied by powers of 10. For example, a number represented as abcd can be written as follows:
abcd = a*10^3 + b*10^2 + c*10^1 + d*10^0
We can separate the digits and rewrite this as:
abcd = a + b + c + d + (a*999 + b*99 + c*9)
abcd = a + b + c + d + 9*(a*111 + b*11 + c)
This implies that any number can be expressed as the sum of its digits plus a multiple of 9.
So, if we take modulo with 9 on each side,
abcd % 9 = (a + b + c + d) % 9 + 0
This means that the remainder when abcd is divided by 9 is equal to the remainder where the sum of its digits (a + b + c + d) is divided by 9.
If the sum of the digits itself consists of more than one digit, we can further express this sum as the sum of its digits plus a multiple of 9. Consequently, taking modulo 9 will eliminate the multiple of 9, until the sum of digits become single digit number.
As a result, the sum of the digits of any number, will equal its modulo 9. If the result of the modulo operation is zero, it indicates that the single-digit result is 9.
To know about code implementation Refer, Digital Root (repeated digital sum) of the given large integer
Similar Reads
Sum of Digits in a^n till a single digit Given two numbers a and n, the task is to find the single sum of digits of a^n (pow(a, n)). In single digit sum, we keep doing sum of digit until a single digit is left. Examples: Input : a = 5, n = 4 Output : 4 5^4 = 625 = 6+2+5 = 13 Since 13 has two digits, we sum again 1 + 3 = 4. Input : a = 2, n
6 min read
Find smallest number with given digits and sum of digits Given two positive integers P and Q, find the minimum integer containing only digits P and Q such that the sum of the digits of the integer is N. Example: Input: N = 11, P = 4, Q = 7 Output: 47Explanation: There are two possible integers that can be formed from 4 and 7 such that their sum is 11 i.e.
9 min read
Maximum of sum and product of digits until number is reduced to a single digit Given a number N, the task is to print the maximum between the sum and multiplication of the digits of the given number until the number is reduced to a single digit. Note: Sum and multiplication of digits to be done until the number is reduced to a single digit. Let's take an example where N = 19,
6 min read
Find smallest number with given number of digits and sum of digits under given constraints Given two integers S and D, the task is to find the number having D number of digits and the sum of its digits as S such that the difference between the maximum and the minimum digit in the number is as minimum as possible. If multiple such numbers are possible, print the smallest number.Examples: I
7 min read
Smallest number with given sum of digits and sum of square of digits Given the sum of digits a and sum of the square of digits b . Find the smallest number with the given sum of digits and the sum of the square of digits. The number should not contain more than 100 digits. Print -1 if no such number exists or if the number of digits is more than 100.Examples: Input :
15+ min read
Number obtained by reducing sum of digits of 2N into a single digit Given a positive integer N, the task is to find the single digit obtained after recursively adding the digits of 2N until a single digit remains. Examples: Input: N = 6Output: 1Explanation: 26 = 64. Sum of digits = 10.Now, Sum of digits = 10. Therefore, sum is 1. Input: N = 10Output: 7Explanation: 2
4 min read
Count of n digit numbers whose sum of digits equals to given sum Given two integers n and sum, the task is to find the count of all n digit numbers with sum of digits equal to sum. Note: Leading 0's are not counted as digits. If there exist no n digit number with sum of digits equal to given sum, print -1.Example: Input: n = 2, sum= 2Output: 2Explanation: The num
15+ min read
Find second smallest number from sum of digits and number of digits Given the sum of digits as S and the number of digits as D, the task is to find the second smallest number Examples: Input: S = 9, D = 2Output: 27Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Input: S = 16, D = 3Output: 178Expla
8 min read
Number of times a number can be replaced by the sum of its digits until it only contains one digit Count the number of times a number can be replaced by the sum of its digits until it only contains one digit and number can be very large.Examples: Input : 10 Output : 1 1 + 0 = 1, so only one times an number can be replaced by its sum . Input : 991 Output : 3 9 + 9 + 1 = 19, 1 + 9 = 10, 1 + 0 = 1 h
7 min read
Count numbers in range whose sum of digits is divisible by XOR of digits Given integers L and R, the task for this problem is to find a number of integers in the range L to R whose sum of digits is divisible by bitwise XOR of digits. print the answer. ( L <= R <= 1018) Note: Bitwise XOR sum zero never divides any other number. Examples: Input: L = 10, R = 15Output:
15+ min read