Skip to content
Related Articles

Related Articles

Improve Article
Digital Root (repeated digital sum) of square of an integer using Digital root of the given integer
  • Last Updated : 20 Apr, 2021

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:
Explanation:
152 = 225, 2+2+5 = 9 

Input: N = 9 
Output:



 

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)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live




My Personal Notes arrow_drop_up
Recommended Articles
Page :