Digital root of a large number using recursion

Here given code implementation process.

/*
    Java Program
    Digital root of a large number using recursion
*/
public class DigitSum
{
	// Find the digital root using recursion process
	public String findDigitalRoot(String num)
	{
		if (num.length() == 1)
		{
			// Returning the result of single digit
			return num;
		}
		int sum = 0;
		// Sum of all digit
		for (int i = 0; i < num.length(); ++i)
		{
			sum += num.charAt(i) - '0';
		}
		return findDigitalRoot("" + 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 :
		Given number : 123456
		    1 + 2 + 3 + 4 + 5 + 6 = 21
		    2 + 1 = 3

		    Result =  3
		*/
		task.digitalRoot("123456");
		/*
		Case B :
		Given number : 123908756245134574732783343268
		    1 + 2 + 3 + 9 + 0 + 8 + 7 + 5 + 6 
		    + 2 + 4 + 5 + 1 + 3 + 4 
		    + 5 + 7 + 4 + 7 + 3 + 2 + 7 
		    + 8 + 3 + 3 + 4 + 3 + 2 + 6 + 8 = 132

		    1 + 3 + 2 = 6
		    Result = 6
		*/
		task.digitalRoot("123908756245134574732783343268");
	}
}

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
    Digital root of a large number using recursion
*/

class DigitSum
{
	public:
		// Find the digital root using recursion process
		string findDigitalRoot(string num)
		{
			if (num.length() == 1)
			{
				// Returning the result of single digit
				return num;
			}
			int sum = 0;
			// Sum of all digit
			for (int i = 0; i < num.length(); ++i)
			{
				sum += num[i] - '0';
			}
			return this->findDigitalRoot(to_string(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 :
	Given number : 123456
	    1 + 2 + 3 + 4 + 5 + 6 = 21
	    2 + 1 = 3
	    Result =  3
	*/
	task->digitalRoot("123456");
	/*
	Case B :
	Given number : 123908756245134574732783343268
	    1 + 2 + 3 + 9 + 0 + 8 + 7 + 5 + 6 
	    + 2 + 4 + 5 + 1 + 3 + 4 
	    + 5 + 7 + 4 + 7 + 3 + 2 + 7 
	    + 8 + 3 + 3 + 4 + 3 + 2 + 6 + 8 = 132
	    1 + 3 + 2 = 6
	    Result = 6
	*/
	task->digitalRoot("123908756245134574732783343268");
	return 0;
}

input

 Given number : 123456
 Digital root : 3
 Given number : 123908756245134574732783343268
 Digital root : 6
// Include namespace system
using System;
/*
    Csharp Program
    Digital root of a large number using recursion
*/
public class DigitSum
{
	// Find the digital root using recursion process
	public String findDigitalRoot(String num)
	{
		if (num.Length == 1)
		{
			// Returning the result of single digit
			return num;
		}
		int sum = 0;
		// Sum of all digit
		for (int i = 0; i < num.Length; ++i)
		{
			sum += num[i] - '0';
		}
		return this.findDigitalRoot("" + 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 :
		Given number : 123456
		    1 + 2 + 3 + 4 + 5 + 6 = 21
		    2 + 1 = 3
		    Result =  3
		*/
		task.digitalRoot("123456");
		/*
		Case B :
		Given number : 123908756245134574732783343268
		    1 + 2 + 3 + 9 + 0 + 8 + 7 + 5 + 6 
		    + 2 + 4 + 5 + 1 + 3 + 4 
		    + 5 + 7 + 4 + 7 + 3 + 2 + 7 
		    + 8 + 3 + 3 + 4 + 3 + 2 + 6 + 8 = 132
		    1 + 3 + 2 = 6
		    Result = 6
		*/
		task.digitalRoot("123908756245134574732783343268");
	}
}

input

 Given number : 123456
 Digital root : 3
 Given number : 123908756245134574732783343268
 Digital root : 6
<?php
/*
    Php Program
    Digital root of a large number using recursion
*/
class DigitSum
{
	// Find the digital root using recursion process
	public	function findDigitalRoot($num)
	{
		if (strlen($num) == 1)
		{
			// Returning the result of single digit
			return $num;
		}
		$sum = 0;
		// Sum of all digit
		for ($i = 0; $i < strlen($num); ++$i)
		{
			$sum += ord($num[$i]) - ord('0');
		}
		return $this->findDigitalRoot(strval($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 :
	Given number : 123456
	    1 + 2 + 3 + 4 + 5 + 6 = 21
	    2 + 1 = 3
	    Result =  3
	*/
	$task->digitalRoot("123456");
	/*
	Case B :
	Given number : 123908756245134574732783343268
	    1 + 2 + 3 + 9 + 0 + 8 + 7 + 5 + 6 
	    + 2 + 4 + 5 + 1 + 3 + 4 
	    + 5 + 7 + 4 + 7 + 3 + 2 + 7 
	    + 8 + 3 + 3 + 4 + 3 + 2 + 6 + 8 = 132
	    1 + 3 + 2 = 6
	    Result = 6
	*/
	$task->digitalRoot("123908756245134574732783343268");
}
main();

input

 Given number : 123456
 Digital root : 3
 Given number : 123908756245134574732783343268
 Digital root : 6
/*
    Node JS Program
    Digital root of a large number using recursion
*/
class DigitSum
{
	// Find the digital root using recursion process
	findDigitalRoot(num)
	{
		if (num.length == 1)
		{
			// Returning the result of single digit
			return num;
		}
		var sum = 0;
		// Sum of all digit
		for (var i = 0; i < num.length; ++i)
		{
			sum += num.charAt(i).charCodeAt(0) - '0'.charCodeAt(0);
		}
		return this.findDigitalRoot("" + 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 :
	Given number : 123456
	    1 + 2 + 3 + 4 + 5 + 6 = 21
	    2 + 1 = 3
	    Result =  3
	*/
	task.digitalRoot("123456");
	/*
	Case B :
	Given number : 123908756245134574732783343268
	    1 + 2 + 3 + 9 + 0 + 8 + 7 + 5 + 6 
	    + 2 + 4 + 5 + 1 + 3 + 4 
	    + 5 + 7 + 4 + 7 + 3 + 2 + 7 
	    + 8 + 3 + 3 + 4 + 3 + 2 + 6 + 8 = 132
	    1 + 3 + 2 = 6
	    Result = 6
	*/
	task.digitalRoot("123908756245134574732783343268");
}
main();

input

 Given number : 123456
 Digital root : 3
 Given number : 123908756245134574732783343268
 Digital root : 6
#    Python 3 Program
#    Digital root of a large number using recursion
class DigitSum :
	#  Find the digital root using recursion process
	def findDigitalRoot(self, num) :
		if (len(num) == 1) :
			#  Returning the result of single digit
			return num
		
		sum = 0
		#  Sum of all digit
		i = 0
		while (i < len(num)) :
			sum += ord(num[i]) - ord('0')
			i += 1
		
		return self.findDigitalRoot(str(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() :
	task = DigitSum()
	# Case A :
	# Given number : 123456
	#    1 + 2 + 3 + 4 + 5 + 6 = 21
	#    2 + 1 = 3
	#    Result =  3
	task.digitalRoot("123456")
	# Case B :
	# Given number : 123908756245134574732783343268
	#    1 + 2 + 3 + 9 + 0 + 8 + 7 + 5 + 6 
	#    + 2 + 4 + 5 + 1 + 3 + 4 
	#    + 5 + 7 + 4 + 7 + 3 + 2 + 7 
	#    + 8 + 3 + 3 + 4 + 3 + 2 + 6 + 8 = 132
	#    1 + 3 + 2 = 6
	#    Result = 6
	task.digitalRoot("123908756245134574732783343268")

if __name__ == "__main__": main()

input

 Given number :  123456
 Digital root :  3
 Given number :  123908756245134574732783343268
 Digital root :  6
#    Ruby Program
#    Digital root of a large number using recursion
class DigitSum 
	#  Find the digital root using recursion process
	def findDigitalRoot(num) 
		if (num.length == 1) 
			#  Returning the result of single digit
			return num
		end

		sum = 0
		#  Sum of all digit
		i = 0
		while (i < num.length) 
			sum += num[i].ord - '0'.ord
			i += 1
		end

		return self.findDigitalRoot(sum.to_s)
	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() 
	task = DigitSum.new()
	# Case A :
	# Given number : 123456
	#    1 + 2 + 3 + 4 + 5 + 6 = 21
	#    2 + 1 = 3
	#    Result =  3
	task.digitalRoot("123456")
	# Case B :
	# Given number : 123908756245134574732783343268
	#    1 + 2 + 3 + 9 + 0 + 8 + 7 + 5 + 6 
	#    + 2 + 4 + 5 + 1 + 3 + 4 
	#    + 5 + 7 + 4 + 7 + 3 + 2 + 7 
	#    + 8 + 3 + 3 + 4 + 3 + 2 + 6 + 8 = 132
	#    1 + 3 + 2 = 6
	#    Result = 6
	task.digitalRoot("123908756245134574732783343268")
end

main()

input

 Given number : 123456
 Digital root : 3
 Given number : 123908756245134574732783343268
 Digital root : 6
import scala.collection.mutable._;
/*
    Scala Program
    Digital root of a large number using recursion
*/
class DigitSum()
{
	// Find the digital root using recursion process
	def findDigitalRoot(num: String): String = {
		if (num.length() == 1)
		{
			// Returning the result of single digit
			return num;
		}
		var sum: Int = 0;
		// Sum of all digit
		var i: Int = 0;
		while (i < num.length())
		{
			sum += num.charAt(i).toInt - '0'.toInt;
			i += 1;
		}
		return findDigitalRoot( sum.toString());
	}
	// 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 :
		Given number : 123456
		    1 + 2 + 3 + 4 + 5 + 6 = 21
		    2 + 1 = 3
		    Result =  3
		*/
		task.digitalRoot("123456");
		/*
		Case B :
		Given number : 123908756245134574732783343268
		    1 + 2 + 3 + 9 + 0 + 8 + 7 + 5 + 6 
		    + 2 + 4 + 5 + 1 + 3 + 4 
		    + 5 + 7 + 4 + 7 + 3 + 2 + 7 
		    + 8 + 3 + 3 + 4 + 3 + 2 + 6 + 8 = 132
		    1 + 3 + 2 = 6
		    Result = 6
		*/
		task.digitalRoot("123908756245134574732783343268");
	}
}

input

 Given number : 123456
 Digital root : 3
 Given number : 123908756245134574732783343268
 Digital root : 6
import Foundation;
/*
    Swift 4 Program
    Digital root of a large number using recursion
*/
class DigitSum
{
	// Find the digital root using recursion process
	func findDigitalRoot(_ n: String) -> String
	{
		if (n.count == 1)
		{
			// Returning the result of single digit
			return n;
		}
      	let num = Array(n);
		var sum = 0;
		// Sum of all digit
		var i = 0;
		while (i < num.count)
		{
			sum += Int(UnicodeScalar(String(num[i]))!.value) 
              	- Int(UnicodeScalar(String("0"))!.value);
			i += 1;
		}
		return self.findDigitalRoot(""
			+ String(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(num));
	}
}
func main()
{
	let task = DigitSum();
	/*
	Case A :
	Given number : 123456
	    1 + 2 + 3 + 4 + 5 + 6 = 21
	    2 + 1 = 3
	    Result =  3
	*/
	task.digitalRoot("123456");
	/*
	Case B :
	Given number : 123908756245134574732783343268
	    1 + 2 + 3 + 9 + 0 + 8 + 7 + 5 + 6 
	    + 2 + 4 + 5 + 1 + 3 + 4 
	    + 5 + 7 + 4 + 7 + 3 + 2 + 7 
	    + 8 + 3 + 3 + 4 + 3 + 2 + 6 + 8 = 132
	    1 + 3 + 2 = 6
	    Result = 6
	*/
	task.digitalRoot("123908756245134574732783343268");
}
main();

input

 Given number :  123456
 Digital root :  3
 Given number :  123908756245134574732783343268
 Digital root :  6
/*
    Kotlin Program
    Digital root of a large number using recursion
*/
class DigitSum
{
	// Find the digital root using recursion process
	fun findDigitalRoot(num: String): String 
	{
		if (num.length == 1)
		{
			// Returning the result of single digit
			return num;
		}
		var sum: Int = 0;
		// Sum of all digit
		var i: Int = 0;
		while (i < num.length)
		{
			sum += num.get(i).toInt() - '0'.toInt();
			i += 1;
		}
		return this.findDigitalRoot(sum.toString());
	}
	// 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 :
	Given number : 123456
	    1 + 2 + 3 + 4 + 5 + 6 = 21
	    2 + 1 = 3
	    Result =  3
	*/
	task.digitalRoot("123456");
	/*
	Case B :
	Given number : 123908756245134574732783343268
	    1 + 2 + 3 + 9 + 0 + 8 + 7 + 5 + 6 
	    + 2 + 4 + 5 + 1 + 3 + 4 
	    + 5 + 7 + 4 + 7 + 3 + 2 + 7 
	    + 8 + 3 + 3 + 4 + 3 + 2 + 6 + 8 = 132
	    1 + 3 + 2 = 6
	    Result = 6
	*/
	task.digitalRoot("123908756245134574732783343268");
}

input

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


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.

New Comment







© 2021, kalkicode.com, All rights reserved