Finding sum of digits of a number until sum becomes single digit
Given a number n, we need to find the sum of its digits such that:
If n < 10
digSum(n) = n
Else
digSum(n) = Sum(digSum(n))
Examples :
Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10,
digSum(x) == 10
Hence ans will be 1+0 = 1
Input : 5674
Output : 4
A brute force approach is to sum all the digits until sum < 10.
Flowchart:

Below is the brute force program to find the sum.
C++
// C++ program to find sum of // digits of a number until // sum becomes single digit. #include<bits/stdc++.h> using namespace std; int digSum(int n) { int sum = 0; // Loop to do sum while // sum is not less than // or equal to 9 while(n > 0 || sum > 9) { if(n == 0) { n = sum; sum = 0; } sum += n % 10; n /= 10; } return sum; } // Driver program to test the above function int main() { int n = 1234; cout << digSum(n); return 0; } |
Java
// Java program to find sum of // digits of a number until // sum becomes single digit. import java.util.*; public class GfG { static int digSum(int n) { int sum = 0; // Loop to do sum while // sum is not less than // or equal to 9 while (n > 0 || sum > 9) { if (n == 0) { n = sum; sum = 0; } sum += n % 10; n /= 10; } return sum; } // Driver code public static void main(String argc[]) { int n = 1234; System.out.println(digSum(n)); } } // This code is contributed by Gitanjali. |
Python
# Python program to find sum of # digits of a number until # sum becomes single digit. import math # method to find sum of digits # of a number until sum becomes # single digit def digSum( n): sum = 0 while(n > 0 or sum > 9): if(n == 0): n = sum sum = 0 sum += n % 10 n /= 10 return sum # Driver method n = 1234print (digSum(n)) # This code is contributed by Gitanjali. |
C#
// C# program to find sum of // digits of a number until // sum becomes single digit. using System; class GFG { static int digSum(int n) { int sum = 0; // Loop to do sum while // sum is not less than // or equal to 9 while (n > 0 || sum > 9) { if (n == 0) { n = sum; sum = 0; } sum += n % 10; n /= 10; } return sum; } // Driver code public static void Main() { int n = 1234; Console.Write(digSum(n)); } } // This code is contributed by nitin mittal |
PHP
<?php // PHP program to find sum of // digits of a number until // sum becomes single digit. function digSum( $n) { $sum = 0; // Loop to do sum while // sum is not less than // or equal to 9 while($n > 0 || $sum > 9) { if($n == 0) { $n = $sum; $sum = 0; } $sum += $n % 10; $n = (int)$n / 10; } return $sum; } // Driver Code $n = 1234; echo digSum($n); // This code is contributed // by aj_36 ?> |
Output :
10
There exists a simple and elegant O(1) solution for this too. The ans is given by simply :-
If n == 0
return 0;
If n % 9 == 0
digSum(n) = 9
Else
digSum(n) = n % 9
Below is the implementation of the above idea :
CPP
#include<bits/stdc++.h> using namespace std; int digSum(int n) { if (n == 0) return 0; return (n % 9 == 0) ? 9 : (n % 9); } // Driver program to test the above function int main() { int n = 9999; cout<<digSum(n); return 0; } |
JAVA
import java.io.*; class GFG { static int digSum(int n) { if (n == 0) return 0; return (n % 9 == 0) ? 9 : (n % 9); } // Driver program to test the above function public static void main (String[] args) { int n = 9999; System.out.println(digSum(n)); } } // This code is contributed by anuj_67. |
Python3
def digSum(n): if (n == 0): return 0 if (n % 9 == 0): return 9 else: (n % 9) # Driver program to test the above function n = 9999print(digSum(n)) # This code is contributed by # Smitha Dinesh Semwal |
C#
using System; class GFG { static int digSum(int n) { if (n == 0) return 0; return (n % 9 == 0) ? 9 : (n % 9); } // Driver Code public static void Main () { int n = 9999; Console.Write(digSum(n)); } } // This code is contributed by aj_36 |
PHP
<?php function digSum($n) { if ($n == 0) return 0; return ($n % 9 == 0) ? 9 : ($n % 9); } // Driver program to test the above function $n = 9999; echo digSum($n); //This code is contributed by anuj_67. ?> |
Output:
9
Related Post :
https://www.geeksforgeeks.org/digital-rootrepeated-digital-sum-given-integer/
This article is contributed by Ayush Khanduri. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Maximum of sum and product of digits until number is reduced to a single digit
- Reduce number to a single digit by subtracting adjacent digits repeatedly
- Sum of Digits in a^n till a single digit
- Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1
- Check whether a number can be expressed as a product of single digit numbers
- Finding n-th number made of prime digits (2, 3, 5 and 7) only
- Finding number of digits in n'th Fibonacci number
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Find last five digits of a given five digit number raised to power five
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Integers from the range that are composed of a single distinct digit
- Shortest path to reach one prime to other by changing single digit at a time
- Squares of numbers with repeated single digits | Set 1 (3, 6 and 9)
- Count of Numbers in Range where first digit is equal to last digit of the number
- Find the remainder when First digit of a number is divided by its Last digit



