Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers
Open In App
Next Article:
Maximum of sum and product of digits until number is reduced to a single digit
Next article icon

Finding sum of digits of a number until sum becomes single digit

Last Updated : 15 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

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

Table of Content

  • [Naive Approach] By Repetitively Adding Digits
  • [Expected Approach] Using Mathematical Formula

[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:

  1. Sum the digits: Start by adding all the digits of the given number.
  2. Check the result: If the sum is a single-digit number (i.e., less than 10), stop and return it.
  3. 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)); 

Output
1

Time Complexity: O(log10n), as we are iterating over the digits of the number.
Auxiliary Space: O(1)

[Expected Approach] Using Mathematical Formula

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



Next Article
Maximum of sum and product of digits until number is reduced to a single digit

A

Ayush Khanduri
Improve
Article Tags :
  • DSA
  • Mathematical
  • Technical Scripter
Practice Tags :
  • Mathematical

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 nu
    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
  • Check if the sum of digits of a number N divides it
    Given a number n, the task is to check if the sum of digits of the given number divides the number or not. Examples: Input : n = 12Output : YesExplanation: Sum of digits = 1+2 =3 and 3 divides 12. Input : n = 15Output : NoExplanation: Sum of digits = 1+5 =6 and 15 % 6 != 0. Using Iterative Method -
    6 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences