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:
Digital Root (repeated digital sum) of square of an integer using Digital root of the given integer
Next article icon

Digital Root (repeated digital sum) of the given large integer

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

The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit.
Given a number, the task is to find its digital root. The input number may be large and it may not be possible to store even if we use long long int.

Examples :

Input : num = “1234”
Output : 1
Explanation : The sum of 1+2+3+4 = 10, digSum(x) == 10,Hence ans will be 1+0 = 1

Input : num = “5674”
Output : 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:

  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.

Example:

For a number like 123

  • First, sum the digits: 1 + 2 + 3 + 4 = 10.
  • Since 10 is not a single digit, sum the digits of 10: 1 + 0 = 1.
  • Now, 1 is a single digit, so we stop and return 1.

To read about code implementation Refer, Finding the sum of digits of a number until the sum becomes a single digit

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

C++
// C++ program to find the digit sum  // using mathematical formula  #include <iostream> using namespace std;  int singleDigit(int n) {          // If given number is zero its       // digit sum will be zero only     if (n == 0)         return 0;            // If result of modulo operation is        // zero then, the digit sum is 9       if(n % 9 == 0)           return 9;             return (n % 9); }  int main() {     int n = 1234;     cout << singleDigit(n);     return 0; } 
C
// C program to find the digit sum  // using mathematical formula  #include <stdio.h>  int singleDigit(int n) {          // If given number is zero its       // digit sum will be zero only     if (n == 0)         return 0;            // If result of modulo operation is        // zero then, the digit sum is 9       if(n % 9 == 0)           return 9;             return (n % 9); }  int main() {     int n = 1234;     printf("%d", singleDigit(n));     return 0; } 
Java
// Java program to find the digit sum  // using mathematical formula  class GfG {     static int singleDigit(int n) {            // If given number is zero its         // digit sum will be zero only         if (n == 0)              return 0;              // If result of modulo operation is          // zero then, the digit sum is 9         if(n % 9 == 0)             return 9;               return (n % 9);     }      public static void main(String[] args) {         int n = 1234;         System.out.println(singleDigit(n));     } } 
Python
# Python program to find the digit sum  # using mathematical formula  def singleDigit(n):        # If given number is zero its     # digit sum will be zero only     if n == 0:          return 0          # If result of modulo operation is      # zero then, the digit sum is 9     if n % 9 == 0:         return 9          return n % 9  if __name__ == "__main__":     n = 1234     print(singleDigit(n)) 
C#
// C# program to find the digit sum  // using mathematical formula  using System;  class GfG {     static int singleDigit(int n) {            // If given number is zero its         // digit sum will be zero only         if (n == 0)              return 0;              // If result of modulo operation is          // zero then, the digit sum is 9         if(n % 9 == 0)             return 9;               return (n % 9);     }      static void Main() {         int n = 1234;         Console.WriteLine(singleDigit(n));     } } 
JavaScript
// JavaScript program to find the digit sum  // using mathematical formula  function singleDigit(n) {        // If given number is zero its     // digit sum will be zero only     if (n === 0)          return 0;          // If result of modulo operation is      // zero then, the digit sum is 9     if(n % 9 === 0)         return 9;          return n % 9; }  // Driver Code const n = 1234; console.log(singleDigit(n)); 

Output
1

Time Complexity: O(1)
Auxiliary Space: O(1)
 



Next Article
Digital Root (repeated digital sum) of square of an integer using Digital root of the given integer

S

Shiv Pratap Singh
Improve
Article Tags :
  • DSA
  • Mathematical
  • large-numbers
  • number-digits
Practice Tags :
  • Mathematical

Similar Reads

  • Digital Root (repeated digital sum) of square of an integer using Digital root of the given integer
    Given an integer N, the task is to find the digital root N2 using the digital root of N. Digital Root of a positive integer is calculated by adding the digits of the integer. If the resultant value is a single digit, then that digit is the digital root. If the resultant value contains two or more di
    6 min read
  • Sum of integers upto N with given unit digit (Set 2)
    Given two integer N and D where 1 ? N ? 1018, the task is to find the sum of all the integers from 1 to N whose unit digit is D.Examples: Input: N = 30, D = 3 Output: 39 3 + 13 + 23 = 39Input: N = 5, D = 7 Output: 0 Approach: In Set 1 we saw two basic approaches to find the required sum, but the com
    4 min read
  • Remove repeated digits in a given number
    Given an integer, remove consecutive repeated digits from it. Examples: Input: x = 12224 Output: 124 Input: x = 124422 Output: 1242 Input: x = 11332 Output: 132 We need to process all digits of n and remove consecutive representations. We can go through all digits by repeatedly dividing n with 10 an
    5 min read
  • Digital Root of a given large number using Recursion
    Given a large number num in the form of string with length as N, the task is to find its digital root. The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains t
    7 min read
  • Find M-th number whose repeated sum of digits of a number is N
    Given two positive integers N and M, The task is to find the M-th number whose sum of digits of a number until the sum becomes a single digit is N. Examples: Input: N = 1, M = 3Output: 19 The first two numbers are 1 and 10. Input: N = 2, M = 5Output: 38 The first four numbers are 2, 11, 20, and 29.
    8 min read
  • Print all n-digit numbers whose sum of digits equals to given sum
    Given number of digits n, print all n-digit numbers whose sum of digits adds upto given sum. Solution should not consider leading 0’s as digits.Examples: Input: N = 2, Sum = 3Output: 12 21 30Input: N = 3, Sum = 6Output: 105 114 123 132 141 150 204 213 222 231 240 303 312 321 330 402 411 420 501 510
    10 min read
  • Total numbers with no repeated digits in a range
    Given a range [Tex]L, R [/Tex]find total such numbers in the given range such that they have no repeated digits. For example: 12 has no repeated digit. 22 has repeated digit. 102, 194 and 213 have no repeated digit. 212, 171 and 4004 have repeated digits. Examples: Input : 10 12 Output : 2 Explanati
    15+ min read
  • Sum of digits of a given number to a given power
    Given a number, we need to find the sum of all the digits of a number which we get after raising the number to a specified power.Examples: Input: number = 5, power = 4 Output: 13 Explanation: Raising 5 to the power 4 we get 625. Now adding all the digits = 6 + 2 + 5 Input: number = 9, power = 5 Outp
    4 min read
  • Smallest integer with digit sum M and multiple of N
    Given two positive integers N and M, the task is to find the smallest positive integer which is divisible by N and whose digit sum is M. Print -1 if no such integer exists within the range of int. Examples: Input: N = 13, M = 32 Output: 8879 8879 is divisible by 13 and its Sum of digits of 8879 is 8
    5 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
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