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 = 9Input: 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 numberint 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 * Nint 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 Codeint main(){ string num = "15"; cout << digitalRootofNSquare(num); return 0;} |
Java
// Java implementation of the// above approachimport java.io.*;class GFG{ // Function to find the digital// root of the numberstatic 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 * Nstatic 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 Codepublic 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 numberdef 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 * Ndef 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 Codeif __name__ == '__main__': num = "15"; # Function call print(digitalRootofNSquare(num));# This code is contributed by shikhasingrajput |
C#
// C# implementation of the// above approachusing System;class GFG{ // Function to find the digital// root of the numberstatic 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 * Nstatic 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 Codepublic 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 numberfunction 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 * Nfunction 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 Codevar num = "15";document.write( digitalRootofNSquare(num));</script> |
9
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


