Sum of two numbers represented as arrays

Here given code implementation process.

// C program for
// Sum of two numbers represented as arrays
#include <stdio.h>

// Display given digit array
void printData(int num[], int n)
{
	for (int i = 0; i < n; ++i)
	{
		printf("  %d", num[i]);
	}
	printf("\n");
}
// Finding the sum of two digit arrays
void sum(int num1[], int num2[], int n, int m)
{
	// Loop controlling variables
	int i = n - 1;
	int j = m - 1;
	// Define some auxiliary variables
	int sum = 0;
	int carry = 0;
	// Use to collect digit sum value
	int result[n];
	// Perform digit sum operation
	while (i >= 0 || j >= 0)
	{
		if (i >= 0 && j >= 0)
		{
			result[i] = (num1[i] + num2[j] + carry) % 10;
			carry = (num1[i] + num2[j] + carry) / 10;
			j--;
		}
		else
		{
			result[i] = (num1[i] + carry) % 10;
			carry = (num1[i] + carry) / 10;
		}
		i--;
	}
	if (carry > 0)
	{
		// When  carry exists
		sum = 1;
	}
	i = 0;
	// Combine resultant sum
	while (i < n)
	{
		sum = (sum *10) + result[i];
		i++;
	}
	// Display given array
	printf("\n Given digit array 1 \n");
	printData(num1, n);
	printf("\n Given digit array 2 \n");
	printData(num2, m);
	// Display calculated sum
	printf("\n Sum :  %d\n", sum);
}
// Handles the request to find sum of two digit array
void findSum(int num1[], int num2[], int n, int m)
{
	// Choose longest array
	if (n > m)
	{
		sum(num1, num2, n, m);
	}
	else
	{
		sum(num2, num1, m, n);
	}
}
int main()
{
	// Define single digit arrays
	int num1[] = {
			9 , 5 , 7 , 2
	};
	int num2[] = {
		9 , 9 , 7 , 8 , 2
	};
	// Get the digit length
	int n = sizeof(num1) / sizeof(num1[0]);
	int m = sizeof(num2) / sizeof(num2[0]);
	// Test
	/* 
		  1  1  1
		-----------------
		     9, 5, 7, 2 
		  9, 9, 7, 8, 2 
		-----------------
		 10  9  3  5  4
		-----------------

		O/P : 109354
	*/
	findSum(num1, num2, n, m);
	return 0;
}

Output

 Given digit array 1
  9  9  7  8  2

 Given digit array 2
  9  5  7  2

 Sum :  109354
// Java Program 
// Sum of two numbers represented as arrays
public class DigitSum
{
	// Display given digit array
	public void printData(int[] num, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			System.out.print("  " + num[i]);
		}
		System.out.print("\n");
	}
	// Finding the sum of two digit arrays
	public void sum(int[] num1, int[] num2, int n, int m)
	{
		// Loop controlling variables
		int i = n - 1;
		int j = m - 1;
		// Define some auxiliary variables
		int sum = 0;
		int carry = 0;
		// Use to collect digit sum value
		int[] result = new int[n];
		// Perform digit sum operation
		while (i >= 0 || j >= 0)
		{
			if (i >= 0 && j >= 0)
			{
				result[i] = (num1[i] + num2[j] + carry) % 10;
				carry = (num1[i] + num2[j] + carry) / 10;
				j--;
			}
			else
			{
				result[i] = (num1[i] + carry) % 10;
				carry = (num1[i] + carry) / 10;
			}
			i--;
		}
		if (carry > 0)
		{
			// When  carry exists
			sum = 1;
		}
		i = 0;
		// Combine resultant sum
		while (i < n)
		{
			sum = (sum * 10) + result[i];
			i++;
		}
		// Display given array
		System.out.print("\n Given digit array 1 \n");
		printData(num1, n);
		System.out.print("\n Given digit array 2 \n");
		printData(num2, m);
		// Display calculated sum
		System.out.print("\n Sum : " + sum + "\n");
	}
	// Handles the request to find sum of two digit array
	public void findSum(int[] num1, int[] num2, int n, int m)
	{
		// Choose longest array
		if (n > m)
		{
			sum(num1, num2, n, m);
		}
		else
		{
			sum(num2, num1, m, n);
		}
	}
	public static void main(String[] args)
	{
		DigitSum task = new DigitSum();
		// Define single digit arrays
		int[] num1 = {
			9 , 5 , 7 , 2
		};
		int[] num2 = {
			9 , 9 , 7 , 8 , 2
		};
		// Get the digit length
		int n = num1.length;
		int m = num2.length;
		// Test
		/* 
		      1  1  1
		    -----------------
		         9, 5, 7, 2 
		      9, 9, 7, 8, 2 
		    -----------------
		     10  9  3  5  4
		    -----------------

		    O/P : 109354
		*/
		task.findSum(num1, num2, n, m);
	}
}

Output

 Given digit array 1
  9  9  7  8  2

 Given digit array 2
  9  5  7  2

 Sum : 109354
// Include header file
#include <iostream>
using namespace std;

// C++ Program
// Sum of two numbers represented as arrays

class DigitSum
{
	public:
		// Display given digit array
		void printData(int num[], int n)
		{
			for (int i = 0; i < n; ++i)
			{
				cout << "  " << num[i];
			}
			cout << "\n";
		}
	// Finding the sum of two digit arrays
	void sum(int num1[], int num2[], int n, int m)
	{
		// Loop controlling variables
		int i = n - 1;
		int j = m - 1;
		// Define some auxiliary variables
		int sum = 0;
		int carry = 0;
		// Use to collect digit sum value
		int *result = new int[n];
		// Perform digit sum operation
		while (i >= 0 || j >= 0)
		{
			if (i >= 0 && j >= 0)
			{
				result[i] = (num1[i] + num2[j] + carry) % 10;
				carry = (num1[i] + num2[j] + carry) / 10;
				j--;
			}
			else
			{
				result[i] = (num1[i] + carry) % 10;
				carry = (num1[i] + carry) / 10;
			}
			i--;
		}
		if (carry > 0)
		{
			// When  carry exists
			sum = 1;
		}
		i = 0;
		// Combine resultant sum
		while (i < n)
		{
			sum = (sum *10) + result[i];
			i++;
		}
		// Display given array
		cout << "\n Given digit array 1 \n";
		this->printData(num1, n);
		cout << "\n Given digit array 2 \n";
		this->printData(num2, m);
		// Display calculated sum
		cout << "\n Sum : " << sum << "\n";
	}
	// Handles the request to find sum of two digit array
	void findSum(int num1[], int num2[], int n, int m)
	{
		// Choose longest array
		if (n > m)
		{
			this->sum(num1, num2, n, m);
		}
		else
		{
			this->sum(num2, num1, m, n);
		}
	}
};
int main()
{
	DigitSum task = DigitSum();
	// Define single digit arrays
	int num1[] = {
		9 , 5 , 7 , 2
	};
	int num2[] = {
		9 , 9 , 7 , 8 , 2
	};
	// Get the digit length
	int n = sizeof(num1) / sizeof(num1[0]);
	int m = sizeof(num2) / sizeof(num2[0]);
	// Test
	/*
	      1  1  1
	    -----------------
	         9, 5, 7, 2 
	      9, 9, 7, 8, 2 
	    -----------------
	     10  9  3  5  4
	    -----------------

	    O/P : 109354
	*/
	task.findSum(num1, num2, n, m);
	return 0;
}

Output

 Given digit array 1
  9  9  7  8  2

 Given digit array 2
  9  5  7  2

 Sum : 109354
// Include namespace system
using System;
// C# Program
// Sum of two numbers represented as arrays
public class DigitSum
{
	// Display given digit array
	public void printData(int[] num, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			Console.Write("  " + num[i]);
		}
		Console.Write("\n");
	}
	// Finding the sum of two digit arrays
	public void sum(int[] num1, int[] num2, int n, int m)
	{
		// Loop controlling variables
		int i = n - 1;
		int j = m - 1;
		// Define some auxiliary variables
		int sum = 0;
		int carry = 0;
		// Use to collect digit sum value
		int[] result = new int[n];
		// Perform digit sum operation
		while (i >= 0 || j >= 0)
		{
			if (i >= 0 && j >= 0)
			{
				result[i] = (num1[i] + num2[j] + carry) % 10;
				carry = (num1[i] + num2[j] + carry) / 10;
				j--;
			}
			else
			{
				result[i] = (num1[i] + carry) % 10;
				carry = (num1[i] + carry) / 10;
			}
			i--;
		}
		if (carry > 0)
		{
			// When  carry exists
			sum = 1;
		}
		i = 0;
		// Combine resultant sum
		while (i < n)
		{
			sum = (sum * 10) + result[i];
			i++;
		}
		// Display given array
		Console.Write("\n Given digit array 1 \n");
		printData(num1, n);
		Console.Write("\n Given digit array 2 \n");
		printData(num2, m);
		// Display calculated sum
		Console.Write("\n Sum : " + sum + "\n");
	}
	// Handles the request to find sum of two digit array
	public void findSum(int[] num1, int[] num2, int n, int m)
	{
		// Choose longest array
		if (n > m)
		{
			sum(num1, num2, n, m);
		}
		else
		{
			sum(num2, num1, m, n);
		}
	}
	public static void Main(String[] args)
	{
		DigitSum task = new DigitSum();
		// Define single digit arrays
		int[] num1 = {
			9 , 5 , 7 , 2
		};
		int[] num2 = {
			9 , 9 , 7 , 8 , 2
		};
		// Get the digit length
		int n = num1.Length;
		int m = num2.Length;
		// Test
		/*
		      1  1  1
		    -----------------
		         9, 5, 7, 2 
		      9, 9, 7, 8, 2 
		    -----------------
		     10  9  3  5  4
		    -----------------
		    O/P : 109354
		*/
		task.findSum(num1, num2, n, m);
	}
}

Output

 Given digit array 1
  9  9  7  8  2

 Given digit array 2
  9  5  7  2

 Sum : 109354
<?php
// Php Program
// Sum of two numbers represented as arrays
class DigitSum
{
	// Display given digit array
	public	function printData( & $num, $n)
	{
		for ($i = 0; $i < $n; ++$i)
		{
			echo "  ". $num[$i];
		}
		echo "\n";
	}
	// Finding the sum of two digit arrays
	public	function sum( & $num1, & $num2, $n, $m)
	{
		// Loop controlling variables
		$i = $n - 1;
		$j = $m - 1;
		// Define some auxiliary variables
		$sum = 0;
		$carry = 0;
		// Use to collect digit sum value
		$result = array_fill(0, $n, 0);
		// Perform digit sum operation
		while ($i >= 0 || $j >= 0)
		{
			if ($i >= 0 && $j >= 0)
			{
				$result[$i] = ($num1[$i] + $num2[$j] + $carry) % 10;
				$carry = intval(($num1[$i] + $num2[$j] + $carry) / 10);
				$j--;
			}
			else
			{
				$result[$i] = ($num1[$i] + $carry) % 10;
				$carry = intval(($num1[$i] + $carry) / 10);
			}
			$i--;
		}
		if ($carry > 0)
		{
			// When  carry exists
			$sum = 1;
		}
		$i = 0;
		// Combine resultant sum
		while ($i < $n)
		{
			$sum = ($sum * 10) + $result[$i];
			$i++;
		}
		// Display given array
		echo "\n Given digit array 1 \n";
		$this->printData($num1, $n);
		echo "\n Given digit array 2 \n";
		$this->printData($num2, $m);
		// Display calculated sum
		echo "\n Sum : ". $sum ."\n";
	}
	// Handles the request to find sum of two digit array
	public	function findSum( & $num1, & $num2, $n, $m)
	{
		// Choose longest array
		if ($n > $m)
		{
			$this->sum($num1, $num2, $n, $m);
		}
		else
		{
			$this->sum($num2, $num1, $m, $n);
		}
	}
}

function main()
{
	$task = new DigitSum();
	// Define single digit arrays
	$num1 = array(9, 5, 7, 2);
	$num2 = array(9, 9, 7, 8, 2);
	// Get the digit length
	$n = count($num1);
	$m = count($num2);
	$task->findSum($num1, $num2, $n, $m);
}
main();

Output

 Given digit array 1
  9  9  7  8  2

 Given digit array 2
  9  5  7  2

 Sum : 109354
// Node Js Program
// Sum of two numbers represented as arrays
class DigitSum
{
	// Display given digit array
	printData(num, n)
	{
		for (var i = 0; i < n; ++i)
		{
			process.stdout.write("  " + num[i]);
		}
		process.stdout.write("\n");
	}
	// Finding the sum of two digit arrays
	sum(num1, num2, n, m)
	{
		// Loop controlling variables
		var i = n - 1;
		var j = m - 1;
		// Define some auxiliary variables
		var sum = 0;
		var carry = 0;
		// Use to collect digit sum value
		var result = Array(n).fill(0);
		// Perform digit sum operation
		while (i >= 0 || j >= 0)
		{
			if (i >= 0 && j >= 0)
			{
				result[i] = (num1[i] + num2[j] + carry) % 10;
				carry = parseInt((num1[i] + num2[j] + carry) / 10);
				j--;
			}
			else
			{
				result[i] = (num1[i] + carry) % 10;
				carry = parseInt((num1[i] + carry) / 10);
			}
			i--;
		}
		if (carry > 0)
		{
			// When  carry exists
			sum = 1;
		}
		i = 0;
		// Combine resultant sum
		while (i < n)
		{
			sum = (sum * 10) + result[i];
			i++;
		}
		// Display given array
		process.stdout.write("\n Given digit array 1 \n");
		this.printData(num1, n);
		process.stdout.write("\n Given digit array 2 \n");
		this.printData(num2, m);
		// Display calculated sum
		process.stdout.write("\n Sum : " + sum + "\n");
	}
	// Handles the request to find sum of two digit array
	findSum(num1, num2, n, m)
	{
		// Choose longest array
		if (n > m)
		{
			this.sum(num1, num2, n, m);
		}
		else
		{
			this.sum(num2, num1, m, n);
		}
	}
}

function main()
{
	var task = new DigitSum();
	// Define single digit arrays
	var num1 = [9, 5, 7, 2];
	var num2 = [9, 9, 7, 8, 2];
	// Get the digit length
	var n = num1.length;
	var m = num2.length;
	// Test
	/*
	      1  1  1
	    -----------------
	         9, 5, 7, 2 
	      9, 9, 7, 8, 2 
	    -----------------
	     10  9  3  5  4
	    -----------------
	    O/P : 109354
	*/
	task.findSum(num1, num2, n, m);
}
main();

Output

 Given digit array 1
  9  9  7  8  2

 Given digit array 2
  9  5  7  2

 Sum : 109354
#  Python 3 Program
#  Sum of two numbers represented as arrays
class DigitSum :
	#  Display given digit list
	def printData(self, num, n) :
		i = 0
		while (i < n) :
			print("  ", num[i], end = "")
			i += 1
		
		print(end = "\n")
	
	#  Finding the sum of two digit lists
	def sum(self, num1, num2, n, m) :
		#  Loop controlling variables
		i = n - 1
		j = m - 1
		#  Define some auxiliary variables
		sum = 0
		carry = 0
		#  Use to collect digit sum value
		result = [0] * (n)
		#  Perform digit sum operation
		while (i >= 0 or j >= 0) :
			if (i >= 0 and j >= 0) :
				result[i] = (num1[i] + num2[j] + carry) % 10
				carry = int((num1[i] + num2[j] + carry) / 10)
				j -= 1
			else :
				result[i] = (num1[i] + carry) % 10
				carry = int((num1[i] + carry) / 10)
			
			i -= 1
		
		if (carry > 0) :
			#  When  carry exists
			sum = 1
		
		i = 0
		#  Combine resultant sum
		while (i < n) :
			sum = (sum * 10) + result[i]
			i += 1
		
		#  Display given list
		print("\n Given digit array 1 ")
		self.printData(num1, n)
		print("\n Given digit array 2 ")
		self.printData(num2, m)
		#  Display calculated sum
		print("\n Sum : ", sum )
	
	#  Handles the request to find sum of two digit list
	def findSum(self, num1, num2, n, m) :
		#  Choose longest list
		if (n > m) :
			self.sum(num1, num2, n, m)
		else :
			self.sum(num2, num1, m, n)
		
	

def main() :
	task = DigitSum()
	#  Define single digit lists
	num1 = [9, 5, 7, 2]
	num2 = [9, 9, 7, 8, 2]
	#  Get the digit length
	n = len(num1)
	m = len(num2)
	#  Test
	# 
	#       1  1  1
	#     -----------------
	#          9, 5, 7, 2 
	#       9, 9, 7, 8, 2 
	#     -----------------
	#      10  9  3  5  4
	#     -----------------
	#     O/P : 109354
	
	task.findSum(num1, num2, n, m)

if __name__ == "__main__": main()

Output

 Given digit array 1
   9   9   7   8   2

 Given digit array 2
   9   5   7   2

 Sum :  109354
#  Ruby Program
#  Sum of two numbers represented as arrays
class DigitSum 
	#  Display given digit array
	def printData(num, n) 
		i = 0
		while (i < n) 
			print("  ", num[i])
			i += 1
		end

		print("\n")
	end

	#  Finding the sum of two digit arrays
	def sum(num1, num2, n, m) 
		#  Loop controlling variables
		i = n - 1
		j = m - 1
		#  Define some auxiliary variables
		sum = 0
		carry = 0
		#  Use to collect digit sum value
		result = Array.new(n) {0}
		#  Perform digit sum operation
		while (i >= 0 || j >= 0) 
			if (i >= 0 && j >= 0) 
				result[i] = (num1[i] + num2[j] + carry) % 10
				carry = (num1[i] + num2[j] + carry) / 10
				j -= 1
			else 
				result[i] = (num1[i] + carry) % 10
				carry = (num1[i] + carry) / 10
			end

			i -= 1
		end

		if (carry > 0) 
			#  When  carry exists
			sum = 1
		end

		i = 0
		#  Combine resultant sum
		while (i < n) 
			sum = (sum * 10) + result[i]
			i += 1
		end

		#  Display given array
		print("\n Given digit array 1 \n")
		self.printData(num1, n)
		print("\n Given digit array 2 \n")
		self.printData(num2, m)
		#  Display calculated sum
		print("\n Sum : ", sum ,"\n")
	end

	#  Handles the request to find sum of two digit array
	def findSum(num1, num2, n, m) 
		#  Choose longest array
		if (n > m) 
			self.sum(num1, num2, n, m)
		else 
			self.sum(num2, num1, m, n)
		end

	end

end

def main() 
	task = DigitSum.new()
	#  Define single digit arrays
	num1 = [9, 5, 7, 2]
	num2 = [9, 9, 7, 8, 2]
	#  Get the digit length
	n = num1.length
	m = num2.length
	#  Test
	# 
	#       1  1  1
	#     -----------------
	#          9, 5, 7, 2 
	#       9, 9, 7, 8, 2 
	#     -----------------
	#      10  9  3  5  4
	#     -----------------
	#     O/P : 109354
	
	task.findSum(num1, num2, n, m)
end

main()

Output

 Given digit array 1 
  9  9  7  8  2

 Given digit array 2 
  9  5  7  2

 Sum : 109354
// Scala Program
// Sum of two numbers represented as arrays
class DigitSum
{
	// Display given digit array
	def printData(num: Array[Int], n: Int): Unit = {
		var i: Int = 0;
		while (i < n)
		{
			print("  " + num(i));
			i += 1;
		}
		print("\n");
	}
	// Finding the sum of two digit arrays
	def sum(num1: Array[Int], num2: Array[Int], n: Int, m: Int): Unit = {
		// Loop controlling variables
		var i: Int = n - 1;
		var j: Int = m - 1;
		// Define some auxiliary variables
		var sum: Int = 0;
		var carry: Int = 0;
		// Use to collect digit sum value
		var result: Array[Int] = Array.fill[Int](n)(0);
		// Perform digit sum operation
		while (i >= 0 || j >= 0)
		{
			if (i >= 0 && j >= 0)
			{
				result(i) = (num1(i) + num2(j) + carry) % 10;
				carry = ((num1(i) + num2(j) + carry) / 10).toInt;
				j -= 1;
			}
			else
			{
				result(i) = (num1(i) + carry) % 10;
				carry = ((num1(i) + carry) / 10).toInt;
			}
			i -= 1;
		}
		if (carry > 0)
		{
			// When  carry exists
			sum = 1;
		}
		i = 0;
		// Combine resultant sum
		while (i < n)
		{
			sum = (sum * 10) + result(i);
			i += 1;
		}
		// Display given array
		print("\n Given digit array 1 \n");
		this.printData(num1, n);
		print("\n Given digit array 2 \n");
		this.printData(num2, m);
		// Display calculated sum
		print("\n Sum : " + sum + "\n");
	}
	// Handles the request to find sum of two digit array
	def findSum(num1: Array[Int], num2: Array[Int], n: Int, m: Int): Unit = {
		// Choose longest array
		if (n > m)
		{
			this.sum(num1, num2, n, m);
		}
		else
		{
			this.sum(num2, num1, m, n);
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: DigitSum = new DigitSum();
		// Define single digit arrays
		var num1: Array[Int] = Array(9, 5, 7, 2);
		var num2: Array[Int] = Array(9, 9, 7, 8, 2);
		// Get the digit length
		var n: Int = num1.length;
		var m: Int = num2.length;
		// Test
		/*
		      1  1  1
		    -----------------
		         9, 5, 7, 2 
		      9, 9, 7, 8, 2 
		    -----------------
		     10  9  3  5  4
		    -----------------
		    O/P : 109354
		*/
		task.findSum(num1, num2, n, m);
	}
}

Output

 Given digit array 1
  9  9  7  8  2

 Given digit array 2
  9  5  7  2

 Sum : 109354
// Swift 4 Program
// Sum of two numbers represented as arrays
class DigitSum
{
	// Display given digit array
	func printData(_ num: [Int], _ n: Int)
	{
		var i: Int = 0;
		while (i < n)
		{
			print("  ", num[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	// Finding the sum of two digit arrays
	func sum(_ num1: [Int], _ num2: [Int], _ n: Int, _ m: Int)
	{
		// Loop controlling variables
		var i: Int = n - 1;
		var j: Int = m - 1;
		// Define some auxiliary variables
		var sum: Int = 0;
		var carry: Int = 0;
		// Use to collect digit sum value
		var result: [Int] = Array(repeating: 0, count: n);
		// Perform digit sum operation
		while (i >= 0 || j >= 0)
		{
			if (i >= 0 && j >= 0)
			{
				result[i] = (num1[i] + num2[j] + carry) % 10;
				carry = (num1[i] + num2[j] + carry) / 10;
				j -= 1;
			}
			else
			{
				result[i] = (num1[i] + carry) % 10;
				carry = (num1[i] + carry) / 10;
			}
			i -= 1;
		}
		if (carry > 0)
		{
			// When  carry exists
			sum = 1;
		}
		i = 0;
		// Combine resultant sum
		while (i < n)
		{
			sum = (sum * 10) + result[i];
			i += 1;
		}
		// Display given array
		print("\n Given digit array 1 ");
		self.printData(num1, n);
		print("\n Given digit array 2 ");
		self.printData(num2, m);
		// Display calculated sum
		print("\n Sum : ", sum );
	}
	// Handles the request to find sum of two digit array
	func findSum(_ num1: [Int], _ num2: [Int], _ n: Int, _ m: Int)
	{
		// Choose longest array
		if (n > m)
		{
			self.sum(num1, num2, n, m);
		}
		else
		{
			self.sum(num2, num1, m, n);
		}
	}
}
func main()
{
	let task: DigitSum = DigitSum();
	// Define single digit arrays
	let num1: [Int] = [9, 5, 7, 2];
	let num2: [Int] = [9, 9, 7, 8, 2];
	// Get the digit length
	let n: Int = num1.count;
	let m: Int = num2.count;
	// Test
	/*
	      1  1  1
	    -----------------
	         9, 5, 7, 2 
	      9, 9, 7, 8, 2 
	    -----------------
	     10  9  3  5  4
	    -----------------
	    O/P : 109354
	*/
	task.findSum(num1, num2, n, m);
}
main();

Output

 Given digit array 1
   9   9   7   8   2

 Given digit array 2
   9   5   7   2

 Sum :  109354
// Kotlin Program
// Sum of two numbers represented as arrays
class DigitSum
{
	// Display given digit array
	fun printData(num: Array < Int > , n: Int): Unit
	{
		var i: Int = 0;
		while (i < n)
		{
			print("  " + num[i]);
			i += 1;
		}
		print("\n");
	}
	// Finding the sum of two digit arrays
	fun sum(num1: Array < Int > , num2: Array < Int > , n: Int, m: Int): Unit
	{
		// Loop controlling variables
		var i: Int = n - 1;
		var j: Int = m - 1;
		// Define some auxiliary variables
		var sum: Int = 0;
		var carry: Int = 0;
		// Use to collect digit sum value
		var result: Array < Int > = Array(n)
		{
			0
		};
		// Perform digit sum operation
		while (i >= 0 || j >= 0)
		{
			if (i >= 0 && j >= 0)
			{
				result[i] = (num1[i] + num2[j] + carry) % 10;
				carry = (num1[i] + num2[j] + carry) / 10;
				j -= 1;
			}
			else
			{
				result[i] = (num1[i] + carry) % 10;
				carry = (num1[i] + carry) / 10;
			}
			i -= 1;
		}
		if (carry > 0)
		{
			// When  carry exists
			sum = 1;
		}
		i = 0;
		// Combine resultant sum
		while (i < n)
		{
			sum = (sum * 10) + result[i];
			i += 1;
		}
		// Display given array
		print("\n Given digit array 1 \n");
		this.printData(num1, n);
		print("\n Given digit array 2 \n");
		this.printData(num2, m);
		// Display calculated sum
		print("\n Sum : " + sum + "\n");
	}
	// Handles the request to find sum of two digit array
	fun findSum(num1: Array < Int > , num2: Array < Int > , n: Int, m: Int): Unit
	{
		// Choose longest array
		if (n > m)
		{
			this.sum(num1, num2, n, m);
		}
		else
		{
			this.sum(num2, num1, m, n);
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var task: DigitSum = DigitSum();
	// Define single digit arrays
	var num1: Array < Int > = arrayOf(9, 5, 7, 2);
	var num2: Array < Int > = arrayOf(9, 9, 7, 8, 2);
	// Get the digit length
	var n: Int = num1.count();
	var m: Int = num2.count();
	// Test
	/*
	      1  1  1
	    -----------------
	         9, 5, 7, 2 
	      9, 9, 7, 8, 2 
	    -----------------
	     10  9  3  5  4
	    -----------------
	    O/P : 109354
	*/
	task.findSum(num1, num2, n, m);
}

Output

 Given digit array 1
  9  9  7  8  2

 Given digit array 2
  9  5  7  2

 Sum : 109354
Digit sum example


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