# Find digital root of a large number efficiently

Here given code implementation process.

``````/*
Java Program
Find digital root of a large number efficiently
*/
public class DigitSum
{
// Find the digital root
public int findDigitalRoot(String num)
{
int sum = 0;
// Sum of all digit
for (int i = 0; i < num.length(); ++i)
{
sum = 1 + (sum + (num.charAt(i) - '0') - 1) % 9;
}
return sum;
}
// Handles the request to find digital root of given string number
public void digitalRoot(String num)
{
// Display given number
System.out.println(" Given number : " + num);
// Display calculated result
System.out.println(" Digital root : " + findDigitalRoot(num));
}
public static void main(String[] args)
{
DigitSum task = new DigitSum();
/*
Case A :
( 1 + ( 0 + (1 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (2 - '0') - 1 ) % 9 ) = 3
( 1 + ( 3 + (3 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (4 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (5 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (6 - '0') - 1 ) % 9 ) = 3
-------------------------------------
Result =  3
*/
/*
Case B :
Given number : 123908756245134574732783343268
( 1 + ( 0 + (1 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (2 - '0') - 1 ) % 9 ) = 3
( 1 + ( 3 + (3 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (9 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (0 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (8 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (7 - '0') - 1 ) % 9 ) = 3
( 1 + ( 3 + (5 - '0') - 1 ) % 9 ) = 8
( 1 + ( 8 + (6 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (2 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (4 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (5 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (1 - '0') - 1 ) % 9 ) = 8
( 1 + ( 8 + (3 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (4 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (5 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (7 - '0') - 1 ) % 9 ) = 9
( 1 + ( 9 + (4 - '0') - 1 ) % 9 ) = 4
( 1 + ( 4 + (7 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (3 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (2 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (7 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (8 - '0') - 1 ) % 9 ) = 4
( 1 + ( 4 + (3 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (3 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (4 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (3 - '0') - 1 ) % 9 ) = 8
( 1 + ( 8 + (2 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (6 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (8 - '0') - 1 ) % 9 ) = 6
-------------------------------------
Result = 6
*/
}
}``````

#### input

`````` Given number : 123456
Digital root : 3
Given number : 123908756245134574732783343268
Digital root : 6``````
``````// Include header file
#include <iostream>
#include <string>
using namespace std;

/*
C++ Program
Find digital root of a large number efficiently
*/

class DigitSum
{
public:
// Find the digital root
int findDigitalRoot(string num)
{
int sum = 0;
// Sum of all digit
for (int i = 0; i < num.length(); ++i)
{
sum = 1 + (sum + (num[i] - '0') - 1) % 9;
}
return sum;
}
// Handles the request to find digital root of given string number
void digitalRoot(string num)
{
// Display given number
cout << " Given number : " << num << endl;
// Display calculated result
cout << " Digital root : " << this->findDigitalRoot(num) << endl;
}
};
int main()
{
DigitSum *task = new DigitSum();
/*
Case A :
( 1 + ( 0 + (1 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (2 - '0') - 1 ) % 9 ) = 3
( 1 + ( 3 + (3 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (4 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (5 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (6 - '0') - 1 ) % 9 ) = 3
-------------------------------------
Result =  3
*/
/*
Case B :
Given number : 123908756245134574732783343268
( 1 + ( 0 + (1 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (2 - '0') - 1 ) % 9 ) = 3
( 1 + ( 3 + (3 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (9 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (0 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (8 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (7 - '0') - 1 ) % 9 ) = 3
( 1 + ( 3 + (5 - '0') - 1 ) % 9 ) = 8
( 1 + ( 8 + (6 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (2 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (4 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (5 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (1 - '0') - 1 ) % 9 ) = 8
( 1 + ( 8 + (3 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (4 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (5 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (7 - '0') - 1 ) % 9 ) = 9
( 1 + ( 9 + (4 - '0') - 1 ) % 9 ) = 4
( 1 + ( 4 + (7 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (3 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (2 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (7 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (8 - '0') - 1 ) % 9 ) = 4
( 1 + ( 4 + (3 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (3 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (4 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (3 - '0') - 1 ) % 9 ) = 8
( 1 + ( 8 + (2 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (6 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (8 - '0') - 1 ) % 9 ) = 6
-------------------------------------
Result = 6
*/
return 0;
}``````

#### input

`````` Given number : 123456
Digital root : 3
Given number : 123908756245134574732783343268
Digital root : 6``````
``````// Include namespace system
using System;
/*
Csharp Program
Find digital root of a large number efficiently
*/
public class DigitSum
{
// Find the digital root
public int findDigitalRoot(String num)
{
int sum = 0;
// Sum of all digit
for (int i = 0; i < num.Length; ++i)
{
sum = 1 + (sum + (num[i] - '0') - 1) % 9;
}
return sum;
}
// Handles the request to find digital root of given string number
public void digitalRoot(String num)
{
// Display given number
Console.WriteLine(" Given number : " + num);
// Display calculated result
Console.WriteLine(" Digital root : " + this.findDigitalRoot(num));
}
public static void Main(String[] args)
{
DigitSum task = new DigitSum();
/*
Case A :
( 1 + ( 0 + (1 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (2 - '0') - 1 ) % 9 ) = 3
( 1 + ( 3 + (3 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (4 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (5 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (6 - '0') - 1 ) % 9 ) = 3
-------------------------------------
Result =  3
*/
/*
Case B :
Given number : 123908756245134574732783343268
( 1 + ( 0 + (1 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (2 - '0') - 1 ) % 9 ) = 3
( 1 + ( 3 + (3 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (9 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (0 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (8 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (7 - '0') - 1 ) % 9 ) = 3
( 1 + ( 3 + (5 - '0') - 1 ) % 9 ) = 8
( 1 + ( 8 + (6 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (2 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (4 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (5 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (1 - '0') - 1 ) % 9 ) = 8
( 1 + ( 8 + (3 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (4 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (5 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (7 - '0') - 1 ) % 9 ) = 9
( 1 + ( 9 + (4 - '0') - 1 ) % 9 ) = 4
( 1 + ( 4 + (7 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (3 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (2 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (7 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (8 - '0') - 1 ) % 9 ) = 4
( 1 + ( 4 + (3 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (3 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (4 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (3 - '0') - 1 ) % 9 ) = 8
( 1 + ( 8 + (2 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (6 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (8 - '0') - 1 ) % 9 ) = 6
-------------------------------------
Result = 6
*/
}
}``````

#### input

`````` Given number : 123456
Digital root : 3
Given number : 123908756245134574732783343268
Digital root : 6``````
``````<?php
/*
Php Program
Find digital root of a large number efficiently
*/
class DigitSum
{
// Find the digital root
public	function findDigitalRoot(\$num)
{
\$sum = 0;
// Sum of all digit
for (\$i = 0; \$i < strlen(\$num); ++\$i)
{
\$sum = 1 + (\$sum + (ord(\$num[\$i]) - ord('0')) - 1) % 9;
}
return \$sum;
}
// Handles the request to find digital root of given string number
public	function digitalRoot(\$num)
{
// Display given number
echo(" Given number : ".\$num.
"\n");
// Display calculated result
echo(" Digital root : ".\$this->findDigitalRoot(\$num).
"\n");
}
}

function main()
{
\$task = new DigitSum();
/*
Case A :
( 1 + ( 0 + (1 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (2 - '0') - 1 ) % 9 ) = 3
( 1 + ( 3 + (3 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (4 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (5 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (6 - '0') - 1 ) % 9 ) = 3
-------------------------------------
Result =  3
*/
/*
Case B :
Given number : 123908756245134574732783343268
( 1 + ( 0 + (1 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (2 - '0') - 1 ) % 9 ) = 3
( 1 + ( 3 + (3 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (9 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (0 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (8 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (7 - '0') - 1 ) % 9 ) = 3
( 1 + ( 3 + (5 - '0') - 1 ) % 9 ) = 8
( 1 + ( 8 + (6 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (2 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (4 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (5 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (1 - '0') - 1 ) % 9 ) = 8
( 1 + ( 8 + (3 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (4 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (5 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (7 - '0') - 1 ) % 9 ) = 9
( 1 + ( 9 + (4 - '0') - 1 ) % 9 ) = 4
( 1 + ( 4 + (7 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (3 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (2 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (7 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (8 - '0') - 1 ) % 9 ) = 4
( 1 + ( 4 + (3 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (3 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (4 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (3 - '0') - 1 ) % 9 ) = 8
( 1 + ( 8 + (2 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (6 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (8 - '0') - 1 ) % 9 ) = 6
-------------------------------------
Result = 6
*/
}
main();``````

#### input

`````` Given number : 123456
Digital root : 3
Given number : 123908756245134574732783343268
Digital root : 6``````
``````/*
Node JS Program
Find digital root of a large number efficiently
*/
class DigitSum
{
// Find the digital root
findDigitalRoot(num)
{
var sum = 0;
// Sum of all digit
for (var i = 0; i < num.length; ++i)
{
sum = 1 + (sum + (
num.charAt(i).charCodeAt(0) - '0'.charCodeAt(0)) - 1) % 9;
}
return sum;
}
// Handles the request to find digital root of given string number
digitalRoot(num)
{
// Display given number
console.log(" Given number : " + num);
// Display calculated result
console.log(" Digital root : " + this.findDigitalRoot(num));
}
}

function main()
{
var task = new DigitSum();
/*
Case A :
( 1 + ( 0 + (1 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (2 - '0') - 1 ) % 9 ) = 3
( 1 + ( 3 + (3 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (4 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (5 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (6 - '0') - 1 ) % 9 ) = 3
-------------------------------------
Result =  3
*/
/*
Case B :
Given number : 123908756245134574732783343268
( 1 + ( 0 + (1 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (2 - '0') - 1 ) % 9 ) = 3
( 1 + ( 3 + (3 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (9 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (0 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (8 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (7 - '0') - 1 ) % 9 ) = 3
( 1 + ( 3 + (5 - '0') - 1 ) % 9 ) = 8
( 1 + ( 8 + (6 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (2 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (4 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (5 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (1 - '0') - 1 ) % 9 ) = 8
( 1 + ( 8 + (3 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (4 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (5 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (7 - '0') - 1 ) % 9 ) = 9
( 1 + ( 9 + (4 - '0') - 1 ) % 9 ) = 4
( 1 + ( 4 + (7 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (3 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (2 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (7 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (8 - '0') - 1 ) % 9 ) = 4
( 1 + ( 4 + (3 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (3 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (4 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (3 - '0') - 1 ) % 9 ) = 8
( 1 + ( 8 + (2 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (6 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (8 - '0') - 1 ) % 9 ) = 6
-------------------------------------
Result = 6
*/
}
main();``````

#### input

`````` Given number : 123456
Digital root : 3
Given number : 123908756245134574732783343268
Digital root : 6``````
``````#    Python 3 Program
#    Find digital root of a large number efficiently
class DigitSum :
#  Find the digital root
def findDigitalRoot(self, num) :
sum = 0
#  Sum of all digit
i = 0
while (i < len(num)) :
sum = 1 + (sum + (ord(num[i]) - ord('0')) - 1) % 9
i += 1

return sum

#  Handles the request to find digital root of given string number
def digitalRoot(self, num) :
#  Display given number
print(" Given number : ", num)
#  Display calculated result
print(" Digital root : ", self.findDigitalRoot(num))

def main() :
# Case A :
#    ( 1 + ( 0 + (1 - '0') - 1 ) % 9 ) = 1
#    ( 1 + ( 1 + (2 - '0') - 1 ) % 9 ) = 3
#    ( 1 + ( 3 + (3 - '0') - 1 ) % 9 ) = 6
#    ( 1 + ( 6 + (4 - '0') - 1 ) % 9 ) = 1
#    ( 1 + ( 1 + (5 - '0') - 1 ) % 9 ) = 6
#    ( 1 + ( 6 + (6 - '0') - 1 ) % 9 ) = 3
#    -------------------------------------
#    Result =  3
# Case B :
# Given number : 123908756245134574732783343268
#    ( 1 + ( 0 + (1 - '0') - 1 ) % 9 ) = 1
#    ( 1 + ( 1 + (2 - '0') - 1 ) % 9 ) = 3
#    ( 1 + ( 3 + (3 - '0') - 1 ) % 9 ) = 6
#    ( 1 + ( 6 + (9 - '0') - 1 ) % 9 ) = 6
#    ( 1 + ( 6 + (0 - '0') - 1 ) % 9 ) = 6
#    ( 1 + ( 6 + (8 - '0') - 1 ) % 9 ) = 5
#    ( 1 + ( 5 + (7 - '0') - 1 ) % 9 ) = 3
#    ( 1 + ( 3 + (5 - '0') - 1 ) % 9 ) = 8
#    ( 1 + ( 8 + (6 - '0') - 1 ) % 9 ) = 5
#    ( 1 + ( 5 + (2 - '0') - 1 ) % 9 ) = 7
#    ( 1 + ( 7 + (4 - '0') - 1 ) % 9 ) = 2
#    ( 1 + ( 2 + (5 - '0') - 1 ) % 9 ) = 7
#    ( 1 + ( 7 + (1 - '0') - 1 ) % 9 ) = 8
#    ( 1 + ( 8 + (3 - '0') - 1 ) % 9 ) = 2
#    ( 1 + ( 2 + (4 - '0') - 1 ) % 9 ) = 6
#    ( 1 + ( 6 + (5 - '0') - 1 ) % 9 ) = 2
#    ( 1 + ( 2 + (7 - '0') - 1 ) % 9 ) = 9
#    ( 1 + ( 9 + (4 - '0') - 1 ) % 9 ) = 4
#    ( 1 + ( 4 + (7 - '0') - 1 ) % 9 ) = 2
#    ( 1 + ( 2 + (3 - '0') - 1 ) % 9 ) = 5
#    ( 1 + ( 5 + (2 - '0') - 1 ) % 9 ) = 7
#    ( 1 + ( 7 + (7 - '0') - 1 ) % 9 ) = 5
#    ( 1 + ( 5 + (8 - '0') - 1 ) % 9 ) = 4
#    ( 1 + ( 4 + (3 - '0') - 1 ) % 9 ) = 7
#    ( 1 + ( 7 + (3 - '0') - 1 ) % 9 ) = 1
#    ( 1 + ( 1 + (4 - '0') - 1 ) % 9 ) = 5
#    ( 1 + ( 5 + (3 - '0') - 1 ) % 9 ) = 8
#    ( 1 + ( 8 + (2 - '0') - 1 ) % 9 ) = 1
#    ( 1 + ( 1 + (6 - '0') - 1 ) % 9 ) = 7
#    ( 1 + ( 7 + (8 - '0') - 1 ) % 9 ) = 6
#    -------------------------------------
#    Result = 6

if __name__ == "__main__": main()``````

#### input

`````` Given number :  123456
Digital root :  3
Given number :  123908756245134574732783343268
Digital root :  6``````
``````#    Ruby Program
#    Find digital root of a large number efficiently
class DigitSum
#  Find the digital root
def findDigitalRoot(num)
sum = 0
#  Sum of all digit
i = 0
while (i < num.length)
sum = 1 + (sum + (num[i].ord - '0'.ord) - 1) % 9
i += 1
end
return sum
end

#  Handles the request to find digital root of given string number
def digitalRoot(num)
#  Display given number
print(" Given number : ", num, "\n")
#  Display calculated result
print(" Digital root : ", self.findDigitalRoot(num), "\n")
end

end

def main()
# Case A :
#    ( 1 + ( 0 + (1 - '0') - 1 ) % 9 ) = 1
#    ( 1 + ( 1 + (2 - '0') - 1 ) % 9 ) = 3
#    ( 1 + ( 3 + (3 - '0') - 1 ) % 9 ) = 6
#    ( 1 + ( 6 + (4 - '0') - 1 ) % 9 ) = 1
#    ( 1 + ( 1 + (5 - '0') - 1 ) % 9 ) = 6
#    ( 1 + ( 6 + (6 - '0') - 1 ) % 9 ) = 3
#    -------------------------------------
#    Result =  3
# Case B :
# Given number : 123908756245134574732783343268
#    ( 1 + ( 0 + (1 - '0') - 1 ) % 9 ) = 1
#    ( 1 + ( 1 + (2 - '0') - 1 ) % 9 ) = 3
#    ( 1 + ( 3 + (3 - '0') - 1 ) % 9 ) = 6
#    ( 1 + ( 6 + (9 - '0') - 1 ) % 9 ) = 6
#    ( 1 + ( 6 + (0 - '0') - 1 ) % 9 ) = 6
#    ( 1 + ( 6 + (8 - '0') - 1 ) % 9 ) = 5
#    ( 1 + ( 5 + (7 - '0') - 1 ) % 9 ) = 3
#    ( 1 + ( 3 + (5 - '0') - 1 ) % 9 ) = 8
#    ( 1 + ( 8 + (6 - '0') - 1 ) % 9 ) = 5
#    ( 1 + ( 5 + (2 - '0') - 1 ) % 9 ) = 7
#    ( 1 + ( 7 + (4 - '0') - 1 ) % 9 ) = 2
#    ( 1 + ( 2 + (5 - '0') - 1 ) % 9 ) = 7
#    ( 1 + ( 7 + (1 - '0') - 1 ) % 9 ) = 8
#    ( 1 + ( 8 + (3 - '0') - 1 ) % 9 ) = 2
#    ( 1 + ( 2 + (4 - '0') - 1 ) % 9 ) = 6
#    ( 1 + ( 6 + (5 - '0') - 1 ) % 9 ) = 2
#    ( 1 + ( 2 + (7 - '0') - 1 ) % 9 ) = 9
#    ( 1 + ( 9 + (4 - '0') - 1 ) % 9 ) = 4
#    ( 1 + ( 4 + (7 - '0') - 1 ) % 9 ) = 2
#    ( 1 + ( 2 + (3 - '0') - 1 ) % 9 ) = 5
#    ( 1 + ( 5 + (2 - '0') - 1 ) % 9 ) = 7
#    ( 1 + ( 7 + (7 - '0') - 1 ) % 9 ) = 5
#    ( 1 + ( 5 + (8 - '0') - 1 ) % 9 ) = 4
#    ( 1 + ( 4 + (3 - '0') - 1 ) % 9 ) = 7
#    ( 1 + ( 7 + (3 - '0') - 1 ) % 9 ) = 1
#    ( 1 + ( 1 + (4 - '0') - 1 ) % 9 ) = 5
#    ( 1 + ( 5 + (3 - '0') - 1 ) % 9 ) = 8
#    ( 1 + ( 8 + (2 - '0') - 1 ) % 9 ) = 1
#    ( 1 + ( 1 + (6 - '0') - 1 ) % 9 ) = 7
#    ( 1 + ( 7 + (8 - '0') - 1 ) % 9 ) = 6
#    -------------------------------------
#    Result = 6
end

main()``````

#### input

`````` Given number : 123456
Digital root : 3
Given number : 123908756245134574732783343268
Digital root : 6
``````
``````import scala.collection.mutable._;
/*
Scala Program
Find digital root of a large number efficiently
*/
class DigitSum()
{
// Find the digital root
def findDigitalRoot(num: String): Int = {
var sum: Int = 0;
// Sum of all digit
var i: Int = 0;
while (i < num.length())
{
sum = 1 + (sum + (num.charAt(i).toInt - '0'.toInt) - 1) % 9;
i += 1;
}
return sum;
}
// Handles the request to find digital root of given string number
def digitalRoot(num: String): Unit = {
// Display given number
println(" Given number : " + num);
// Display calculated result
println(" Digital root : " + findDigitalRoot(num));
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: DigitSum = new DigitSum();
/*
Case A :
( 1 + ( 0 + (1 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (2 - '0') - 1 ) % 9 ) = 3
( 1 + ( 3 + (3 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (4 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (5 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (6 - '0') - 1 ) % 9 ) = 3
-------------------------------------
Result =  3
*/
/*
Case B :
Given number : 123908756245134574732783343268
( 1 + ( 0 + (1 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (2 - '0') - 1 ) % 9 ) = 3
( 1 + ( 3 + (3 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (9 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (0 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (8 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (7 - '0') - 1 ) % 9 ) = 3
( 1 + ( 3 + (5 - '0') - 1 ) % 9 ) = 8
( 1 + ( 8 + (6 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (2 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (4 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (5 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (1 - '0') - 1 ) % 9 ) = 8
( 1 + ( 8 + (3 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (4 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (5 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (7 - '0') - 1 ) % 9 ) = 9
( 1 + ( 9 + (4 - '0') - 1 ) % 9 ) = 4
( 1 + ( 4 + (7 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (3 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (2 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (7 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (8 - '0') - 1 ) % 9 ) = 4
( 1 + ( 4 + (3 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (3 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (4 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (3 - '0') - 1 ) % 9 ) = 8
( 1 + ( 8 + (2 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (6 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (8 - '0') - 1 ) % 9 ) = 6
-------------------------------------
Result = 6
*/
}
}``````

#### input

`````` Given number : 123456
Digital root : 3
Given number : 123908756245134574732783343268
Digital root : 6``````
``````import Foundation;
/*
Swift 4 Program
Find digital root of a large number efficiently
*/
class DigitSum
{
// Find the digital root
func findDigitalRoot(_ num: [Character]) -> Int
{
var sum = 0;
// Sum of all digit
var i = 0;
while (i < num.count)
{
sum = 1 + (sum +
(Int(UnicodeScalar(String(num[i]))!.value)
-
Int(UnicodeScalar(String("0"))!.value)) - 1) % 9;
i += 1;
}
return sum;
}
// Handles the request to find digital root of given string number
func digitalRoot(_ num: String)
{
// Display given number
print(" Given number : ", num);
// Display calculated result
print(" Digital root : ", self.findDigitalRoot(Array(num)));
}
}
func main()
{
let task = DigitSum();
/*
Case A :
( 1 + ( 0 + (1 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (2 - '0') - 1 ) % 9 ) = 3
( 1 + ( 3 + (3 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (4 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (5 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (6 - '0') - 1 ) % 9 ) = 3
-------------------------------------
Result =  3
*/
/*
Case B :
Given number : 123908756245134574732783343268
( 1 + ( 0 + (1 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (2 - '0') - 1 ) % 9 ) = 3
( 1 + ( 3 + (3 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (9 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (0 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (8 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (7 - '0') - 1 ) % 9 ) = 3
( 1 + ( 3 + (5 - '0') - 1 ) % 9 ) = 8
( 1 + ( 8 + (6 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (2 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (4 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (5 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (1 - '0') - 1 ) % 9 ) = 8
( 1 + ( 8 + (3 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (4 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (5 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (7 - '0') - 1 ) % 9 ) = 9
( 1 + ( 9 + (4 - '0') - 1 ) % 9 ) = 4
( 1 + ( 4 + (7 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (3 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (2 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (7 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (8 - '0') - 1 ) % 9 ) = 4
( 1 + ( 4 + (3 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (3 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (4 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (3 - '0') - 1 ) % 9 ) = 8
( 1 + ( 8 + (2 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (6 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (8 - '0') - 1 ) % 9 ) = 6
-------------------------------------
Result = 6
*/
}
main();``````

#### input

`````` Given number :  123456
Digital root :  3
Given number :  123908756245134574732783343268
Digital root :  6``````
``````/*
Kotlin Program
Find digital root of a large number efficiently
*/
class DigitSum
{
// Find the digital root
fun findDigitalRoot(num: String): Int
{
var sum: Int = 0;
// Sum of all digit
var i: Int = 0;
while (i < num.length)
{
sum = 1 + (sum + (num.get(i).toInt() - '0'.toInt()) - 1) % 9;
i += 1;
}
return sum;
}
// Handles the request to find digital root of given string number
fun digitalRoot(num: String): Unit
{
// Display given number
println(" Given number : " + num);
// Display calculated result
println(" Digital root : " + this.findDigitalRoot(num));
}
}
fun main(args: Array < String > ): Unit
{
val task: DigitSum = DigitSum();
/*
Case A :
( 1 + ( 0 + (1 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (2 - '0') - 1 ) % 9 ) = 3
( 1 + ( 3 + (3 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (4 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (5 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (6 - '0') - 1 ) % 9 ) = 3
-------------------------------------
Result =  3
*/
/*
Case B :
Given number : 123908756245134574732783343268
( 1 + ( 0 + (1 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (2 - '0') - 1 ) % 9 ) = 3
( 1 + ( 3 + (3 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (9 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (0 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (8 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (7 - '0') - 1 ) % 9 ) = 3
( 1 + ( 3 + (5 - '0') - 1 ) % 9 ) = 8
( 1 + ( 8 + (6 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (2 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (4 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (5 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (1 - '0') - 1 ) % 9 ) = 8
( 1 + ( 8 + (3 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (4 - '0') - 1 ) % 9 ) = 6
( 1 + ( 6 + (5 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (7 - '0') - 1 ) % 9 ) = 9
( 1 + ( 9 + (4 - '0') - 1 ) % 9 ) = 4
( 1 + ( 4 + (7 - '0') - 1 ) % 9 ) = 2
( 1 + ( 2 + (3 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (2 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (7 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (8 - '0') - 1 ) % 9 ) = 4
( 1 + ( 4 + (3 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (3 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (4 - '0') - 1 ) % 9 ) = 5
( 1 + ( 5 + (3 - '0') - 1 ) % 9 ) = 8
( 1 + ( 8 + (2 - '0') - 1 ) % 9 ) = 1
( 1 + ( 1 + (6 - '0') - 1 ) % 9 ) = 7
( 1 + ( 7 + (8 - '0') - 1 ) % 9 ) = 6
-------------------------------------
Result = 6
*/
}``````

#### input

`````` Given number : 123456
Digital root : 3
Given number : 123908756245134574732783343268
Digital root : 6``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.