Difference between two large numbers

Here given code implementation process.

/*
   Java Program
   Difference between two large numbers
*/
class Differences
{
	// Check that given first number is larger than second
	public boolean isFirstLargest(String num1, String num2)
	{
		// Get the size
		int n1 = num1.length();
		int n2 = num2.length();
		if (n1 > n2)
		{
			return true;
		}
		if (n2 > n1)
		{
			return false;
		}
		// When have number string size is equal size
		// Check which is largest
		for (int i = 0; i < n1; ++i)
		{
			if (num1.charAt(i) > num2.charAt(i))
			{
				return true;
			}
			else if (num1.charAt(i) < num2.charAt(i))
			{
				return false;
			}
		}
		return true;
	}
	// Handles the request of find difference of given two string numbers
	public void findDifference(String num1, String num2)
	{
		// Get the size
		int n1 = num1.length() - 1;
		int n2 = num2.length() - 1;
		int carry = 0;
		int digit = 0;
		String result = "";
		// Compare digit from the end
		while (n1 >= 0 || n2 >= 0)
		{
			if (n1 >= 0 && n2 >= 0)
			{
				// When both number digit are exists
				// Subtract digit from end
				digit = (num1.charAt(n1) - '0') - (num2.charAt(n2) - '0') - carry;
				if (digit < 0)
				{
					digit += 10;
					carry = 1;
				}
				else
				{
					carry = 0;
				}
				// reduce length
				n1--;
				n2--;
			}
			else
			{
				// When the number is present in the first number
				digit = (num1.charAt(n1) - '0') - carry;
				if (digit < 0)
				{
					digit += 10;
					carry = 1;
				}
				else
				{
					carry = 0;
				}
				// reduce length
				n1--;
			}
			// Combine result
			result = "" + digit + result;
		}
		System.out.print("\nGiven Numbers (" + num1 + "," + num2 + ") \n");
		// Display calculated result
		System.out.print("Difference : " + result + "\n");
	}
	public void difference(String num1, String num2)
	{
		if (isFirstLargest(num1, num2) == false)
		{
			// When num2 is largest
			findDifference(num2, num1);
		}
		else
		{
			findDifference(num1, num2);
		}
	}
	public static void main(String[] args)
	{
		Differences task = new Differences();
		// Test case
		task.difference("10", "5");
		task.difference("4330", "544");
		task.difference("30", "5633");
		task.difference("4444465334454777", "7777454534234555454");
	}
}

Output

Given Numbers (10,5)
Difference : 05

Given Numbers (4330,544)
Difference : 3786

Given Numbers (5633,30)
Difference : 5603

Given Numbers (7777454534234555454,4444465334454777)
Difference : 7773010068900100677
#include <iostream>
#include <string>
using namespace std;

// Check that given first number is larger than second
bool isFirstLargest(string num1, string num2)
{
	// Get the size
	int n1 = num1.size();
	int n2 = num2.size();
	if (n1 > n2)
	{
		return true;
	}
	if (n2 > n1)
	{
		return false;
	}
	// When have number string size is equal size
	// Check which is largest
	for (int i = 0; i < n1; ++i)
	{
		if (num1[i] > num2[i])
		{
			return true;
		}
		else if (num1[i] < num2[i])
		{
			return false;
		}
	}
	return true;
}
// Handles the request of find difference of given two string numbers
void findDifference(string num1, string num2)
{
	// Get the size
	int n1 = num1.size() - 1;
	int n2 = num2.size() - 1;
	int carry = 0;
	int digit = 0;
	string result = "";
	// Compare digit from the end
	while (n1 >= 0 || n2 >= 0)
	{
		if (n1 >= 0 && n2 >= 0)
		{
			// When both number digit are exists
			// Subtract digit from end
			digit = (num1[n1] - '0') - (num2[n2] - '0') - carry;
			if (digit < 0)
			{
				digit += 10;
				carry = 1;
			}
			else
			{
				carry = 0;
			}
			// reduce length
			n1--;
			n2--;
		}
		else
		{
			// When the number is present in the first number
			digit = (num1[n1] - '0') - carry;
			if (digit < 0)
			{
				digit += 10;
				carry = 1;
			}
			else
			{
				carry = 0;
			}
			// reduce length
			n1--;
		}
		// Combine result
		result = to_string(digit) + result;
	}
	cout << "\nGiven Numbers (" << num1 << "," << num2 << ") " << endl;
	// Display calculated result
	cout <<"Difference : "<< result << endl;
}
void difference(string num1, string num2)
{
	if (isFirstLargest(num1, num2) == false)
	{
		// When num2 is largest
		findDifference(num2, num1);
	}
	else
	{
		findDifference(num1, num2);
	}
}
int main(int argc, char
	const *argv[])
{
	// Test
	difference("10", "5");
	difference("4330", "544");
	difference("30", "5633");
	difference("4444465334454777", "7777454534234555454");
	return 0;
}
// When if to_string() not work then 
// g++ -std=c++11 source.cpp [Compile by]
// ./a.out [Run by]

Output

Given Numbers (10,5)
Difference : 05

Given Numbers (4330,544)
Difference : 3786

Given Numbers (5633,30)
Difference : 5603

Given Numbers (7777454534234555454,4444465334454777)
Difference : 7773010068900100677
// Include namespace system
using System;
/*
   C# Program
   Difference between two large numbers
*/
public class Differences
{
	// Check that given first number is larger than second
	public Boolean isFirstLargest(String num1, String num2)
	{
		// Get the size
		int n1 = num1.Length;
		int n2 = num2.Length;
		if (n1 > n2)
		{
			return true;
		}
		if (n2 > n1)
		{
			return false;
		}
		// When have number string size is equal size
		// Check which is largest
		for (int i = 0; i < n1; ++i)
		{
			if (num1[i] > num2[i])
			{
				return true;
			}
			else if (num1[i] < num2[i])
			{
				return false;
			}
		}
		return true;
	}
	// Handles the request of find difference of given two string numbers
	public void findDifference(String num1, String num2)
	{
		// Get the size
		int n1 = num1.Length - 1;
		int n2 = num2.Length - 1;
		int carry = 0;
		int digit = 0;
		String result = "";
		// Compare digit from the end
		while (n1 >= 0 || n2 >= 0)
		{
			if (n1 >= 0 && n2 >= 0)
			{
				// When both number digit are exists
				// Subtract digit from end
				digit = (num1[n1] - '0') - (num2[n2] - '0') - carry;
				if (digit < 0)
				{
					digit += 10;
					carry = 1;
				}
				else
				{
					carry = 0;
				}
				// reduce length
				n1--;
				n2--;
			}
			else
			{
				// reduce length
				// When the number is present in the first number
				digit = (num1[n1] - '0') - carry;
				if (digit < 0)
				{
					digit += 10;
					carry = 1;
				}
				else
				{
					carry = 0;
				}
				n1--;
			}
			// Combine result
			result = "" + digit + result;
		}
		Console.Write("\nGiven Numbers (" + num1 + "," + num2 + ") \n");
		// Display calculated result
		Console.Write("Difference : " + result + "\n");
	}
	public void difference(String num1, String num2)
	{
		if (isFirstLargest(num1, num2) == false)
		{
			// When num2 is largest
			findDifference(num2, num1);
		}
		else
		{
			findDifference(num1, num2);
		}
	}
	public static void Main(String[] args)
	{
		Differences task = new Differences();
		// Test case
		task.difference("10", "5");
		task.difference("4330", "544");
		task.difference("30", "5633");
		task.difference("4444465334454777", "7777454534234555454");
	}
}

Output

Given Numbers (10,5)
Difference : 05

Given Numbers (4330,544)
Difference : 3786

Given Numbers (5633,30)
Difference : 5603

Given Numbers (7777454534234555454,4444465334454777)
Difference : 7773010068900100677
<?php
/*
   Php Program
   Difference between two large numbers
*/
class Differences
{
	// Check that given first number is larger than second
	public	function isFirstLargest($num1, $num2)
	{
		// Get the size
		$n1 = strlen($num1);
		$n2 = strlen($num2);
		if ($n1 > $n2)
		{
			return true;
		}
		if ($n2 > $n1)
		{
			return false;
		}
		// When have number string size is equal size
		// Check which is largest
		for ($i = 0; $i < $n1; ++$i)
		{
			if (ord($num1[$i]) > ord($num2[$i]))
			{
				return true;
			}
			else if (ord($num1[$i]) < ord($num2[$i]))
			{
				return false;
			}
		}
		return true;
	}
	// Handles the request of find difference of given two string numbers
	public	function findDifference($num1, $num2)
	{
		// Get the size
		$n1 = strlen($num1) - 1;
		$n2 = strlen($num2) - 1;
		$carry = 0;
		$digit = 0;
		$result = "";
		// Compare digit from the end
		while ($n1 >= 0 || $n2 >= 0)
		{
			if ($n1 >= 0 && $n2 >= 0)
			{
				// When both number digit are exists
				// Subtract digit from end
				$digit = (ord($num1[$n1]) - ord('0')) - (ord($num2[$n2]) - ord('0')) - $carry;
				if ($digit < 0)
				{
					$digit += 10;
					$carry = 1;
				}
				else
				{
					$carry = 0;
				}
				// reduce length
				$n1--;
				$n2--;
			}
			else
			{
				// reduce length
				// When the number is present in the first number
				$digit = (ord($num1[$n1]) - ord('0')) - $carry;
				if ($digit < 0)
				{
					$digit += 10;
					$carry = 1;
				}
				else
				{
					$carry = 0;
				}
				$n1--;
			}
			// Combine result
			$result = "". $digit . $result;
		}
		echo "\nGiven Numbers (". $num1 .",". $num2 .") \n";
		// Display calculated result
		echo "Difference : ". $result ."\n";
	}
	public	function difference($num1, $num2)
	{
		if ($this->isFirstLargest($num1, $num2) == false)
		{
			// When num2 is largest
			$this->findDifference($num2, $num1);
		}
		else
		{
			$this->findDifference($num1, $num2);
		}
	}
}

function main()
{
	$task = new Differences();
	// Test case
	$task->difference("10", "5");
	$task->difference("4330", "544");
	$task->difference("30", "5633");
	$task->difference("4444465334454777", "7777454534234555454");
}
main();

Output

Given Numbers (10,5)
Difference : 05

Given Numbers (4330,544)
Difference : 3786

Given Numbers (5633,30)
Difference : 5603

Given Numbers (7777454534234555454,4444465334454777)
Difference : 7773010068900100677
/*
   Node Js Program
   Difference between two large numbers
*/
class Differences
{
	// Check that given first number is larger than second
	isFirstLargest(num1, num2)
	{
		// Get the size
		var n1 = num1.length;
		var n2 = num2.length;
		if (n1 > n2)
		{
			return true;
		}
		if (n2 > n1)
		{
			return false;
		}
		// When have number string size is equal size
		// Check which is largest
		for (var i = 0; i < n1; ++i)
		{
			if ((num1[i]).charCodeAt(0) > (num2[i]).charCodeAt(0))
			{
				return true;
			}
			else if ((num1[i]).charCodeAt(0) < (num2[i]).charCodeAt(0))
			{
				return false;
			}
		}
		return true;
	}
	// Handles the request of find difference of given two string numbers
	findDifference(num1, num2)
	{
		// Get the size
		var n1 = num1.length - 1;
		var n2 = num2.length - 1;
		var carry = 0;
		var digit = 0;
		var result = "";
		// Compare digit from the end
		while (n1 >= 0 || n2 >= 0)
		{
			if (n1 >= 0 && n2 >= 0)
			{
				// When both number digit are exists
				// Subtract digit from end
				digit = ((num1[n1]).charCodeAt(0) - ('0').charCodeAt(0)) - ((num2[n2]).charCodeAt(0) - ('0').charCodeAt(0)) - carry;
				if (digit < 0)
				{
					digit += 10;
					carry = 1;
				}
				else
				{
					carry = 0;
				}
				// reduce length
				n1--;
				n2--;
			}
			else
			{
				// reduce length
				// When the number is present in the first number
				digit = ((num1[n1]).charCodeAt(0) - ('0').charCodeAt(0)) - carry;
				if (digit < 0)
				{
					digit += 10;
					carry = 1;
				}
				else
				{
					carry = 0;
				}
				n1--;
			}
			// Combine result
			result = "" + digit + result;
		}
		process.stdout.write("\nGiven Numbers (" + num1 + "," + num2 + ") \n");
		// Display calculated result
		process.stdout.write("Difference : " + result + "\n");
	}
	difference(num1, num2)
	{
		if (this.isFirstLargest(num1, num2) == false)
		{
			// When num2 is largest
			this.findDifference(num2, num1);
		}
		else
		{
			this.findDifference(num1, num2);
		}
	}
}

function main()
{
	var task = new Differences();
	// Test case
	task.difference("10", "5");
	task.difference("4330", "544");
	task.difference("30", "5633");
	task.difference("4444465334454777", "7777454534234555454");
}
main();

Output

Given Numbers (10,5)
Difference : 05

Given Numbers (4330,544)
Difference : 3786

Given Numbers (5633,30)
Difference : 5603

Given Numbers (7777454534234555454,4444465334454777)
Difference : 7773010068900100677
#    Python 3 Program
#    Difference between two large numbers

class Differences :
	#  Check that given first number is larger than second
	def isFirstLargest(self, num1, num2) :
		#  Get the size
		n1 = len(num1)
		n2 = len(num2)
		if (n1 > n2) :
			return True
		
		if (n2 > n1) :
			return False
		
		#  When have number string size is equal size
		#  Check which is largest
		i = 0
		while (i < n1) :
			if (ord(num1[i]) > ord(num2[i])) :
				return True
			
			elif(ord(num1[i]) < ord(num2[i])) :
				return False
			
			i += 1
		
		return True
	
	#  Handles the request of find difference of given two string numbers
	def findDifference(self, num1, num2) :
		#  Get the size
		n1 = len(num1) - 1
		n2 = len(num2) - 1
		carry = 0
		digit = 0
		result = ""
		#  Compare digit from the end
		while (n1 >= 0 or n2 >= 0) :
			if (n1 >= 0 and n2 >= 0) :
				#  When both number digit are exists
				#  Subtract digit from end
				digit = (ord(num1[n1]) - ord('0')) - (ord(num2[n2]) - ord('0')) - carry
				if (digit < 0) :
					digit += 10
					carry = 1
				else :
					carry = 0
				
				#  reduce length
				n1 -= 1
				n2 -= 1
			else :
				#  reduce length
				#  When the number is present in the first number
				digit = (ord(num1[n1]) - ord('0')) - carry
				if (digit < 0) :
					digit += 10
					carry = 1
				else :
					carry = 0
				
				n1 -= 1
			
			#  Combine result
			result =  str(digit) + result
		
		print("\nGiven Numbers (", num1 ,",", num2 ,") ")
		#  Display calculated result
		print("Difference : ", result )
	
	def difference(self, num1, num2) :
		if (self.isFirstLargest(num1, num2) == False) :
			#  When num2 is largest
			self.findDifference(num2, num1)
		else :
			self.findDifference(num1, num2)
		
	

def main() :
	task = Differences()
	#  Test case
	task.difference("10", "5")
	task.difference("4330", "544")
	task.difference("30", "5633")
	task.difference("4444465334454777", "7777454534234555454")

if __name__ == "__main__": main()

Output

Given Numbers ( 10 , 5 )
Difference :  05

Given Numbers ( 4330 , 544 )
Difference :  3786

Given Numbers ( 5633 , 30 )
Difference :  5603

Given Numbers ( 7777454534234555454 , 4444465334454777 )
Difference :  7773010068900100677
#  Ruby Program
#  Difference between two large numbers

class Differences 
	#  Check that given first number is larger than second
	def isFirstLargest(num1, num2) 
		#  Get the size
		n1 = num1.length()
		n2 = num2.length()
		if (n1 > n2) 
			return true
		end

		if (n2 > n1) 
			return false
		end

		#  When have number string size is equal size
		#  Check which is largest
		i = 0
		while (i < n1) 
			if ((num1[i]).ord > (num2[i]).ord) 
				return true
			elsif((num1[i]).ord < (num2[i]).ord) 
				return false
			end

			i += 1
		end

		return true
	end

	#  Handles the request of find difference of given two string numbers
	def findDifference(num1, num2) 
		#  Get the size
		n1 = num1.length() - 1
		n2 = num2.length() - 1
		carry = 0
		digit = 0
		result = ""
		#  Compare digit from the end
		while (n1 >= 0 || n2 >= 0) 
			if (n1 >= 0 && n2 >= 0) 
				#  When both number digit are exists
				#  Subtract digit from end
				digit = ((num1[n1]).ord - ('0').ord) - ((num2[n2]).ord - ('0').ord) - carry
				if (digit < 0) 
					digit += 10
					carry = 1
				else 
					carry = 0
				end

				#  reduce length
				n1 -= 1
				n2 -= 1
			else 
				#  reduce length
				#  When the number is present in the first number
				digit = ((num1[n1]).ord - ('0').ord) - carry
				if (digit < 0) 
					digit += 10
					carry = 1
				else 
					carry = 0
				end

				n1 -= 1
			end

			#  Combine result
			result = digit.to_s + result
		end

		print("\nGiven Numbers (", num1 ,",", num2 ,") \n")
		#  Display calculated result
		print("Difference : ", result ,"\n")
	end

	def difference(num1, num2) 
		if (self.isFirstLargest(num1, num2) == false) 
			#  When num2 is largest
			self.findDifference(num2, num1)
		else 
			self.findDifference(num1, num2)
		end

	end

end

def main() 
	task = Differences.new()
	#  Test case
	task.difference("10", "5")
	task.difference("4330", "544")
	task.difference("30", "5633")
	task.difference("4444465334454777", "7777454534234555454")
end

main()

Output

Given Numbers (10,5) 
Difference : 05

Given Numbers (4330,544) 
Difference : 3786

Given Numbers (5633,30) 
Difference : 5603

Given Numbers (7777454534234555454,4444465334454777) 
Difference : 7773010068900100677
/*
   Scala Program
   Difference between two large numbers
*/
class Differences
{
	// Check that given first number is larger than second
	def isFirstLargest(num1: String, num2: String): Boolean = {
		// Get the size
		var n1: Int = num1.length();
		var n2: Int = num2.length();
		if (n1 > n2)
		{
			return true;
		}
		if (n2 > n1)
		{
			return false;
		}
		// When have number string size is equal size
		// Check which is largest
		var i: Int = 0;
		while (i < n1)
		{
			if (num1(i) > num2(i))
			{
				return true;
			}
			else if (num1(i) < num2(i))
			{
				return false;
			}
			i += 1;
		}
		return true;
	}
	// Handles the request of find difference of given two string numbers
	def findDifference(num1: String, num2: String): Unit = {
		// Get the size
		var n1: Int = num1.length() - 1;
		var n2: Int = num2.length() - 1;
		var carry: Int = 0;
		var digit: Int = 0;
		var result: String = "";
		// Compare digit from the end
		while (n1 >= 0 || n2 >= 0)
		{
			if (n1 >= 0 && n2 >= 0)
			{
				// When both number digit are exists
				// Subtract digit from end
				digit = (num1(n1) - '0') - (num2(n2) - '0') - carry;
				if (digit < 0)
				{
					digit += 10;
					carry = 1;
				}
				else
				{
					carry = 0;
				}
				// reduce length
				n1 -= 1;
				n2 -= 1;
			}
			else
			{
				// reduce length
				// When the number is present in the first number
				digit = (num1(n1) - '0') - carry;
				if (digit < 0)
				{
					digit += 10;
					carry = 1;
				}
				else
				{
					carry = 0;
				}
				n1 -= 1;
			}
			// Combine result
			result = "" + digit + result;
		}
		print("\nGiven Numbers (" + num1 + "," + num2 + ") \n");
		// Display calculated result
		print("Difference : " + result + "\n");
	}
	def difference(num1: String, num2: String): Unit = {
		if (this.isFirstLargest(num1, num2) == false)
		{
			// When num2 is largest
			this.findDifference(num2, num1);
		}
		else
		{
			this.findDifference(num1, num2);
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Differences = new Differences();
		// Test case
		task.difference("10", "5");
		task.difference("4330", "544");
		task.difference("30", "5633");
		task.difference("4444465334454777", "7777454534234555454");
	}
}

Output

Given Numbers (10,5)
Difference : 05

Given Numbers (4330,544)
Difference : 3786

Given Numbers (5633,30)
Difference : 5603

Given Numbers (7777454534234555454,4444465334454777)
Difference : 7773010068900100677
/*
   Swift 4 Program
   Difference between two large numbers
*/
class Differences
{
    // Check that given first number is larger than second
    func isFirstLargest(_ num1: String, _ num2: String)->Bool
    {
        // Get the size
        let n1: Int = num1.count;
        let n2: Int = num2.count;
        let s1 = Array(num1);
        let s2 = Array(num2);
        if (n1 > n2)
        {
            return true;
        }
        if (n2 > n1)
        {
            return false;
        }
        // When have number string size is equal size
        // Check which is largest
        var i: Int = 0;
        while (i < n1)
        {
            if (Int(UnicodeScalar(String(s1[i]))!.value) > Int(UnicodeScalar(String(s2[i]))!.value))
            {
                return true;
            }
            else if (Int(UnicodeScalar(String(s1[i]))!.value) < Int(UnicodeScalar(String(s2[i]))!.value))
            {
                return false;
            }
            i += 1;
        }
        return true;
    }
    // Handles the request of find difference of given two string numbers
    func findDifference(_ num1: String, _ num2: String)
    {
        // Get the size
        var n1: Int = num1.count - 1;
        var n2: Int = num2.count - 1;
        var carry: Int = 0;
        var digit: Int = 0;
        let s1 = Array(num1);
        let s2 = Array(num2);
        var result: String = "";
        // Compare digit from the end
        while (n1 >= 0 || n2 >= 0)
        {
            if (n1 >= 0 && n2 >= 0)
            {
                // When both number digit are exists
                // Subtract digit from end
                digit = (Int(UnicodeScalar(String(s1[n1]))!.value) - Int(UnicodeScalar("0")!.value)) - (Int(UnicodeScalar(String(s2[n2]))!.value) - Int(UnicodeScalar("0")!.value)) - carry;
                if (digit < 0)
                {
                    digit += 10;
                    carry = 1;
                }
                else
                {
                    carry = 0;
                }
                // reduce length
                n1 -= 1;
                n2 -= 1;
            }
            else
            {
                // reduce length
                // When the number is present in the first number
                digit = Int(UnicodeScalar(String(s1[n1]))!.value) - Int(UnicodeScalar("0")!.value) - carry;
                if (digit < 0)
                {
                    digit += 10;
                    carry = 1;
                }
                else
                {
                    carry = 0;
                }
                n1 -= 1;
            }
     
            // Combine result
            result = String(digit) + result;
        }
        print("\nGiven Numbers (", num1, ",", num2, ") ");
        // Display calculated result
        print("Difference : ", result);
    }
    func difference(_ num1: String, _ num2: String)
    {
        if (self.isFirstLargest(num1, num2) == false)
        {
            // When num2 is largest
            self.findDifference(num2, num1);
        }
        else
        {
            self.findDifference(num1, num2);
        }
    }
}
func main()
{
    let task: Differences = Differences();
    // Test case
    task.difference("10", "5");
    task.difference("4330", "544");
    task.difference("30", "5633");
    task.difference("4444465334454777", "7777454534234555454");
}
main();

Output

Given Numbers ( 10 , 5 )
Difference :  05

Given Numbers ( 4330 , 544 )
Difference :  3786

Given Numbers ( 5633 , 30 )
Difference :  5603

Given Numbers ( 7777454534234555454 , 4444465334454777 )
Difference :  7773010068900100677
/*
   Kotlin Program
   Difference between two large numbers
*/
class Differences
{
	// Check that given first number is larger than second
	fun isFirstLargest(num1: String, num2: String): Boolean
	{
		// Get the size
		var n1: Int = num1.count();
		var n2: Int = num2.count();
		if (n1 > n2)
		{
			return true;
		}
		if (n2 > n1)
		{
			return false;
		}
		// When have number string size is equal size
		// Check which is largest
		var i: Int = 0;
		while (i < n1)
		{
			if (num1[i] > num2[i])
			{
				return true;
			}
			else if (num1[i] < num2[i])
			{
				return false;
			}
			i += 1;
		}
		return true;
	}
	// Handles the request of find difference of given two string numbers
	fun findDifference(num1: String, num2: String): Unit
	{
		// Get the size
		var n1: Int = num1.count() - 1;
		var n2: Int = num2.count() - 1;
		var carry: Int = 0;
		var digit: Int ;
		var result: String = "";
		// Compare digit from the end
		while (n1 >= 0 || n2 >= 0)
		{
			if (n1 >= 0 && n2 >= 0)
			{
				// When both number digit are exists
				// Subtract digit from end
				digit = (num1[n1] - '0') - (num2[n2] - '0') - carry;
				if (digit < 0)
				{
					digit += 10;
					carry = 1;
				}
				else
				{
					carry = 0;
				}
				// reduce length
				n1 -= 1;
				n2 -= 1;
			}
			else
			{
				// reduce length
				// When the number is present in the first number
				digit = (num1[n1] - '0') - carry;
				if (digit < 0)
				{
					digit += 10;
					carry = 1;
				}
				else
				{
					carry = 0;
				}
				n1 -= 1;
			}
			// Combine result
			result = "" + digit + result;
		}
		print("\nGiven Numbers (" + num1 + "," + num2 + ") \n");
		// Display calculated result
		print("Difference : " + result + "\n");
	}
	fun difference(num1: String, num2: String): Unit
	{
		if (this.isFirstLargest(num1, num2) == false)
		{
			// When num2 is largest
			this.findDifference(num2, num1);
		}
		else
		{
			this.findDifference(num1, num2);
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Differences = Differences();
	// Test case
	task.difference("10", "5");
	task.difference("4330", "544");
	task.difference("30", "5633");
	task.difference("4444465334454777", "7777454534234555454");
}

Output

Given Numbers (10,5)
Difference : 05

Given Numbers (4330,544)
Difference : 3786

Given Numbers (5633,30)
Difference : 5603

Given Numbers (7777454534234555454,4444465334454777)
Difference : 7773010068900100677

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