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 of a given large number using Recursion
Next article icon

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

Last Updated : 20 Apr, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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 digits, those digits are summed and the process is repeated until a single-digit is obtained.

Examples:

Input: N = 15 
Output: 9 
Explanation:
152 = 225, 2+2+5 = 9 

Input: N = 9 
Output: 9 

 

Approach: The idea is to find the Digital Root of N. Now we can find the digital root of N2 using the digital root of N by observing the below points : 

  • If the digital root of N is 1 or 8 then the digital root of N2 is always 1;
  • If the digital root of N is 2 or 7 then the digital root of N2 is always 4;
  • If the digital root of N is 3 or 6 or 9 then the digital root of N2 is always 9;
  • If the digital root of N is 4 or 5 then the digital root of N2 is always 7;

Below is the implementation of the above approach:

C++




// C++ implementation of the
// above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the digital
// root of the number
int digitalRootofN(string num)
{
    // If num is 0
    if (num.compare("0") == 0)
        return 0;
 
    // Count sum of digits under mod 9
    int ans = 0;
    for (int i = 0; i < num.length(); i++)
        ans = (ans + num[i] - '0') % 9;
 
    // If digit sum is multiple of 9,
    // 9, else remainder with 9.
    return (ans == 0) ? 9 : ans % 9;
}
 
// Returns digital root of N * N
int digitalRootofNSquare(string N)
{
    // finding digital root of N
    int NDigRoot = digitalRootofN(N);
 
    if (NDigRoot == 1 || NDigRoot == 8)
        return 1;
 
    if (NDigRoot == 2 || NDigRoot == 7)
        return 4;
 
    if (NDigRoot == 3 || NDigRoot == 6)
        return 9;
 
    if (NDigRoot == 4 || NDigRoot == 5)
        return 7;
}
 
// Driver Code
int main()
{
    string num = "15";
    cout << digitalRootofNSquare(num);
 
    return 0;
}
 
 

Java




// Java implementation of the
// above approach
import java.io.*;
 
class GFG{
  
// Function to find the digital
// root of the number
static int digitalRootofN(String num)
{
     
    // If num is 0
    if (num.compareTo("0") == 0)
        return 0;
  
    // Count sum of digits under mod 9
    int ans = 0;
    for(int i = 0; i < num.length(); i++)
        ans = (ans + num.charAt(i) - '0') % 9;
  
    // If digit sum is multiple of 9,
    // 9, else remainder with 9.
    return (ans == 0) ? 9 : ans % 9;
}
  
// Returns digital root of N * N
static int digitalRootofNSquare(String N)
{
     
    // Finding digital root of N
    int NDigRoot = digitalRootofN(N);
  
    if (NDigRoot == 1 || NDigRoot == 8)
        return 1;
  
    else if (NDigRoot == 2 || NDigRoot == 7)
        return 4;
  
    else if (NDigRoot == 3 || NDigRoot == 6)
        return 9;
    else
        return 7;
}
 
// Driver Code
public static void main (String[] args)
{
    String num = "15";
     
    // Function call
    System.out.print(digitalRootofNSquare(num));
}
}
 
// This code is contributed by code_hunt
 
 

Python3




# Python3 implementation of the
# above approach
 
# Function to find the digital
# root of the number
def digitalRootofN(num):
 
    # If num is 0
    if (num == ("0")):
        return 0;
 
    # Count sum of digits
    # under mod 9
    ans = 0;
    for i in range(0, len(num)):
        ans = (ans + ord(num[i]) - ord('0')) % 9;
 
    # If digit sum is multiple of 9,
    # 9, else remainder with 9.
    return 9 if (ans == 0) else ans % 9;
 
# Returns digital root of N * N
def digitalRootofNSquare(N):
 
    # Finding digital root of N
    NDigRoot = digitalRootofN(N);
    if (NDigRoot == 1 or NDigRoot == 8):
        return 1;
    elif(NDigRoot == 2 or NDigRoot == 7):
        return 4;
    elif(NDigRoot == 3 or NDigRoot == 6):
        return 9;
    else:
        return 7;
 
# Driver Code
if __name__ == '__main__':
   
    num = "15";
 
    # Function call
    print(digitalRootofNSquare(num));
 
# This code is contributed by shikhasingrajput
 
 

C#




// C# implementation of the
// above approach
using System;
 
class GFG{
  
// Function to find the digital
// root of the number
static int digitalRootofN(string num)
{
     
    // If num is 0
    if (num.CompareTo("0") == 0)
        return 0;
  
    // Count sum of digits under mod 9
    int ans = 0;
    for(int i = 0; i < num.Length; i++)
        ans = (ans + num[i] - '0') % 9;
  
    // If digit sum is multiple of 9,
    // 9, else remainder with 9.
    return (ans == 0) ? 9 : ans % 9;
}
  
// Returns digital root of N * N
static int digitalRootofNSquare(string N)
{
     
    // Finding digital root of N
    int NDigRoot = digitalRootofN(N);
  
    if (NDigRoot == 1 || NDigRoot == 8)
        return 1;
  
    else if (NDigRoot == 2 || NDigRoot == 7)
        return 4;
  
    else if (NDigRoot == 3 || NDigRoot == 6)
        return 9;
  
    else
        return 7;
}
 
// Driver Code
public static void Main ()
{
    string num = "15";
     
    // Function call
    Console.Write(digitalRootofNSquare(num));
}
}
 
// This code is contributed by code_hunt
 
 

Javascript




<script>
 
// Javascript implementation of the
// above approach
 
// Function to find the digital
// root of the number
function digitalRootofN(num)
{
    // If num is 0
    if (num == "0")
        return 0;
 
    // Count sum of digits under mod 9
    var ans = 0;
    for (var i = 0; i < num.length; i++)
        ans = (ans + num[i] - '0') % 9;
 
    // If digit sum is multiple of 9,
    // 9, else remainder with 9.
    return (ans == 0) ? 9 : ans % 9;
}
 
// Returns digital root of N * N
function digitalRootofNSquare(N)
{
    // finding digital root of N
    var NDigRoot = digitalRootofN(N);
 
    if (NDigRoot == 1 || NDigRoot == 8)
        return 1;
 
    if (NDigRoot == 2 || NDigRoot == 7)
        return 4;
 
    if (NDigRoot == 3 || NDigRoot == 6)
        return 9;
 
    if (NDigRoot == 4 || NDigRoot == 5)
        return 7;
}
 
// Driver Code
var num = "15";
document.write( digitalRootofNSquare(num));
 
</script>
 
 
Output: 
9

 

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



Next Article
Digital Root of a given large number using Recursion
author
spp____
Improve
Article Tags :
  • DSA
  • Mathematical
Practice Tags :
  • Mathematical

Similar Reads

  • Digital Root (repeated digital sum) of the given large integer
    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 ne
    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
  • Smallest N digit number whose sum of square of digits is a Perfect Square
    Given an integer N, find the smallest N digit number such that the sum of the square of digits (in decimal representation) of the number is also a perfect square. If no such number exists, print -1.Examples: Input : N = 2 Output : 34 Explanation: The smallest possible 2 digit number whose sum of squ
    15+ min read
  • Find all numbers between range L to R such that sum of digit and sum of square of digit is prime
    Given the range L and R, count all numbers between L to R such that sum of digits of each number and sum of square of digits of each number is Prime.Note: 10 <= [L, R] <= 108 Examples: Input: L = 10, R = 20 Output: 4 Such types of numbers are: 11 12 14 16 Input: L = 100, R = 130 Output: 9Such
    9 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
  • Check if N can be represented as sum of squares of two consecutive integers
    Given an integer N, the task is to check whether N can be represented as a sum of squares of two consecutive integers or not. Examples: Input: N = 5 Output: Yes Explanation: The integer 5 = 12 + 22 where 1 and 2 are consecutive numbers. Input: 13 Output: Yes Explanation: 13 = 22 + 32 Approach: This
    6 min read
  • Check if the sum of a subarray within a given range is a perfect square or not
    Given an array arr[] of size N and an array range[], the task is to check if the sum of the subarray {range[0], .. , range[1]} is a perfect square or not. If the sum is a perfect square, then print the square root of the sum. Otherwise, print -1. Example : Input: arr[] = {2, 19, 33, 48, 90, 100}, ra
    7 min read
  • Count of ways to represent N as sum of a prime number and twice of a square
    Given an integer N, the task is to count the number of ways so that N can be written as the sum of a prime number and twice of a square, i.e. [Tex]N = 2*A^{2} + P [/Tex] , where P can be any prime number and A is any positive integer. Note: [Tex]N <= 10^{6} [/Tex] Examples: Input: N = 9 Output: 1
    8 min read
  • Find square root of number upto given precision using binary search
    Given a positive number n and precision p, find the square root of number upto p decimal places using binary search. Note : Prerequisite : Binary search Examples: Input : number = 50, precision = 3Output : 7.071Input : number = 10, precision = 4Output : 3.1622 We have discussed how to compute the in
    9 min read
  • Count of numbers whose sum of increasing powers of digits is equal to the number itself
    Given an integer N, the task is to count all the integers less than or equal to N that follow the property where the sum of their digits raised to the power (starting from 1 and increased by 1 each time) is equal to the integer itself i.e. if D1D2D3...DN is an N digit number then for it to satisfy t
    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