Add two integers of given base

Here given code implementation process.

// Include header file
#include <iostream>
using namespace std;
// C++ program
// Add two integers of given base
class Addition
{
	public: char charValue(int num)
	{
		if (num >= 0 && num <= 9)
		{
			return (char)(num + '0');
		}
		else
		{
			return (char)(num - 10 + 'A');
		}
	}
	// Convert given number into decimal number
	int convertToDecimal(int num, int baseValue)
	{
		if (baseValue == 10)
		{
			return num;
		}
		int n = num;
		if (n < 0)
		{
			n = -n;
		}
		int multiplier = 1;
		int result = 0;
		while (n != 0)
		{
			result += (n % 10) *multiplier;
			multiplier *= baseValue;
			n = n / 10;
		}
		if (num < 0)
		{
			return -result;
		}
		return result;
	}
	void additionOfNumber(int num1, int num2, int baseValue)
	{
		// Convert Given numbers into decimal and addition of it value
		int sum = this->convertToDecimal(num1, baseValue) + 
          		  this->convertToDecimal(num2, baseValue);
		bool flag = false;
		if (sum < 0)
		{
			sum = -sum;
			flag = true;
		}
		// This is used to store result
		string result = "";
		// Transform sum to given base
		while (sum > 0)
		{
			result = (this->charValue(sum % baseValue))  +  result;
			sum /= baseValue;
		}
		if (flag == true)
		{
			result = "-" + result;
		}
		// Display given numbers
		cout << "\n Given num1 : " 
      		 << num1;
		cout << "\n Given num2 : " 
             << num2;
		cout << "\n Given Base : " 
             << baseValue;
		// Display result
		cout << "\n Addition : " << result << "\n";
	}
};
int main() 
{
	Addition *task = new Addition();
	// Test A
	// Base : 16
	// 16 + 5 = 1B (hexa)
	// Note (22 + 5) = 27 (in decimal)
	task->additionOfNumber(16, 5, 16);
	// Test B
	// Base : 4
	// 42 + 14 = 123 (Base 4)
	task->additionOfNumber(42, 15, 4);
	// Test C
	// Base : 8
	// 453 + 234 = 707 (Base 8)
	task->additionOfNumber(453, 234, 8);
	// Test C
	// Base : 3
	// (-45) + (-23) = -222 (Base 3)
	task->additionOfNumber(-45, -23, 3);
	return 0;
}

Output

 Given num1 : 16
 Given num2 : 5
 Given Base : 16
 Addition : 1B

 Given num1 : 42
 Given num2 : 15
 Given Base : 4
 Addition : 123

 Given num1 : 453
 Given num2 : 234
 Given Base : 8
 Addition : 707

 Given num1 : -45
 Given num2 : -23
 Given Base : 3
 Addition : -222
// Java program
// Add two integers of given base
class Addition
{
	public char charValue(int num)
	{
		if (num >= 0 && num <= 9)
		{
			return (char)(num + '0');
		}
		else
		{
			return (char)(num - 10 + 'A');
		}
	}
	// Convert given number into decimal number
	public int convertToDecimal(int num, int baseValue)
	{
		if (baseValue == 10)
		{
			return num;
		}
		int n = num;
		if (n < 0)
		{
			n = -n;
		}
		int multiplier = 1;
		int result = 0;
		while (n != 0)
		{
			result += (n % 10) * multiplier;
			multiplier *= baseValue;
			n = n / 10;
		}
		if (num < 0)
		{
			return -result;
		}
		return result;
	}
	public void additionOfNumber(int num1, int num2, int baseValue)
	{
		// Convert Given numbers into decimal and addition of it value
		int sum = convertToDecimal(num1, baseValue) + 
          		  convertToDecimal(num2, baseValue);
		boolean flag = false;
		if (sum < 0)
		{
			sum = -sum;
			flag = true;
		}
		// This is used to store result
		String result = "";
		// Transform sum to given base
		while (sum > 0)
		{
			result = (charValue(sum % baseValue)) + result;
			sum /= baseValue;
		}
		if (flag == true)
		{
			result = "-" + result;
		}
		// Display given numbers
		System.out.print("\n Given num1 : " + num1);
		System.out.print("\n Given num2 : " + num2);
		System.out.print("\n Given Base : " + baseValue);
		// Display result
		System.out.print("\n Addition : " + result + "\n");
	}
	public static void main(String[] args) 
	{
		Addition task = new Addition();
		// Test A
		// Base : 16
		// 16 + 5 = 1B (hexa)
		// Note (22 + 5) = 27 (in decimal)
		task.additionOfNumber(16, 5, 16);
		// Test B
		// Base : 4
		// 42 + 14 = 123 (Base 4)
		task.additionOfNumber(42, 15, 4);
		// Test C
		// Base : 8
		// 453 + 234 = 707 (Base 8)
		task.additionOfNumber(453, 234, 8);
		// Test C
		// Base : 3
		// (-45) + (-23) = -222 (Base 3)
		task.additionOfNumber(-45, -23, 3);
	}
}

Output

 Given num1 : 16
 Given num2 : 5
 Given Base : 16
 Addition : 1B

 Given num1 : 42
 Given num2 : 15
 Given Base : 4
 Addition : 123

 Given num1 : 453
 Given num2 : 234
 Given Base : 8
 Addition : 707

 Given num1 : -45
 Given num2 : -23
 Given Base : 3
 Addition : -222
// Include namespace system
using System;
// Csharp program
// Add two integers of given base
public class Addition
{
	public char charValue(int num)
	{
		if (num >= 0 && num <= 9)
		{
			return (char)(num + '0');
		}
		else
		{
			return (char)(num - 10 + 'A');
		}
	}
	// Convert given number into decimal number
	public int convertToDecimal(int num, int baseValue)
	{
		if (baseValue == 10)
		{
			return num;
		}
		int n = num;
		if (n < 0)
		{
			n = -n;
		}
		int multiplier = 1;
		int result = 0;
		while (n != 0)
		{
			result += (n % 10) * multiplier;
			multiplier *= baseValue;
			n = n / 10;
		}
		if (num < 0)
		{
			return -result;
		}
		return result;
	}
	public void additionOfNumber(int num1, int num2, int baseValue)
	{
		// Convert Given numbers into decimal and addition of it value
		int sum = this.convertToDecimal(num1, baseValue) + 
                  this.convertToDecimal(num2, baseValue);
		Boolean flag = false;
		if (sum < 0)
		{
			sum = -sum;
			flag = true;
		}
		// This is used to store result
		String result = "";
		// Transform sum to given base
		while (sum > 0)
		{
			result = (this.charValue(sum % baseValue)) + result;
			sum /= baseValue;
		}
		if (flag == true)
		{
			result = "-" + result;
		}
		// Display given numbers
		Console.Write("\n Given num1 : " + num1);
		Console.Write("\n Given num2 : " + num2);
		Console.Write("\n Given Base : " + baseValue);
		// Display result
		Console.Write("\n Addition : " + result + "\n");
	}
	public static void Main(String[] args)
	{
		Addition task = new Addition();
		// Test A
		// Base : 16
		// 16 + 5 = 1B (hexa)
		// Note (22 + 5) = 27 (in decimal)
		task.additionOfNumber(16, 5, 16);
		// Test B
		// Base : 4
		// 42 + 14 = 123 (Base 4)
		task.additionOfNumber(42, 15, 4);
		// Test C
		// Base : 8
		// 453 + 234 = 707 (Base 8)
		task.additionOfNumber(453, 234, 8);
		// Test C
		// Base : 3
		// (-45) + (-23) = -222 (Base 3)
		task.additionOfNumber(-45, -23, 3);
	}
}

Output

 Given num1 : 16
 Given num2 : 5
 Given Base : 16
 Addition : 1B

 Given num1 : 42
 Given num2 : 15
 Given Base : 4
 Addition : 123

 Given num1 : 453
 Given num2 : 234
 Given Base : 8
 Addition : 707

 Given num1 : -45
 Given num2 : -23
 Given Base : 3
 Addition : -222
package main
import "fmt"
// Go program
// Add two integers of given base
type Addition struct {}
func getAddition() * Addition {
	var me *Addition = &Addition {}
	return me
}
func(this Addition) charValue(num int) byte {
	if num >= 0 && num <= 9 {
		return (byte)(num + 48)
	} else {
		return (byte)(num - 10 + 65)
	}
}
// Convert given number into decimal number
func(this Addition) convertToDecimal(num, baseValue int) int {
	if baseValue == 10 {
		return num
	}
	var n int = num
	if n < 0 {
		n = -n
	}
	var multiplier int = 1
	var result int = 0
	for (n != 0) {
		result += (n % 10) * multiplier
		multiplier *= baseValue
		n = n / 10
	}
	if num < 0 {
		return -result
	}
	return result
}
func(this Addition) additionOfNumber(num1, num2, baseValue int) {
	// Convert Given numbers into decimal and addition of it value
	var sum int = this.convertToDecimal(num1, baseValue) + 
				  this.convertToDecimal(num2, baseValue)
	var flag bool = false
	if sum < 0 {
		sum = -sum
		flag = true
	}
	// This is used to store result
	var result string = ""
	// Transform sum to given base
	for (sum > 0) {
		result = string((this.charValue(sum % baseValue))) + result
		sum = sum / baseValue
	}
	if flag == true {
		result = "-" + result
	}
	// Display given numbers
	fmt.Print("\n Given num1 : ", num1)
	fmt.Print("\n Given num2 : ", num2)
	fmt.Print("\n Given Base : ", baseValue)
	// Display result
	fmt.Print("\n Addition : ", result, "\n")
}
func main() {
	var task * Addition = getAddition()
	// Test A
	// Base : 16
	// 16 + 5 = 1B (hexa)
	// Note (22 + 5) = 27 (in decimal)
	task.additionOfNumber(16, 5, 16)
	// Test B
	// Base : 4
	// 42 + 14 = 123 (Base 4)
	task.additionOfNumber(42, 15, 4)
	// Test C
	// Base : 8
	// 453 + 234 = 707 (Base 8)
	task.additionOfNumber(453, 234, 8)
	// Test C
	// Base : 3
	// (-45) + (-23) = -222 (Base 3)
	task.additionOfNumber(-45, -23, 3)
}

Output

 Given num1 : 16
 Given num2 : 5
 Given Base : 16
 Addition : 1B

 Given num1 : 42
 Given num2 : 15
 Given Base : 4
 Addition : 123

 Given num1 : 453
 Given num2 : 234
 Given Base : 8
 Addition : 707

 Given num1 : -45
 Given num2 : -23
 Given Base : 3
 Addition : -222
<?php
// Php program
// Add two integers of given base
class Addition
{
	public	function charValue($num)
	{
		if ($num >= 0 && $num <= 9)
		{
			return chr($num + ord('0'));
		}
		else
		{
			return chr($num - 10 + ord('A'));
		}
	}
	// Convert given number into decimal number
	public	function convertToDecimal($num, $baseValue)
	{
		if ($baseValue == 10)
		{
			return $num;
		}
		$n = $num;
		if ($n < 0)
		{
			$n = -$n;
		}
		$multiplier = 1;
		$result = 0;
		while ($n != 0)
		{
			$result += ($n % 10) * $multiplier;
			$multiplier *= $baseValue;
			$n = (int)($n / 10);
		}
		if ($num < 0)
		{
			return -$result;
		}
		return $result;
	}
	public	function additionOfNumber($num1, $num2, $baseValue)
	{
		// Convert Given numbers into decimal and addition of it value
		$sum = $this->convertToDecimal($num1, $baseValue) + 
               $this->convertToDecimal($num2, $baseValue);
		$flag = false;
		if ($sum < 0)
		{
			$sum = -$sum;
			$flag = true;
		}
		// This is used to store result
		$result = "";
		// Transform sum to given base
		while ($sum > 0.00000)
		{
			$result = $this->charValue($sum % $baseValue).$result;
			$sum = (int)($sum / $baseValue);
		}
		if ($flag == true)
		{
			$result = "-".$result;
		}
		// Display given numbers
		echo("\n Given num1 : ".$num1);
		echo("\n Given num2 : ".$num2);
		echo("\n Given Base : ".$baseValue);
		// Display result
		echo("\n Addition : ".$result.
			"\n");
	}
}

function main()
{
	$task = new Addition();
	// Test A
	// Base : 16
	// 16 + 5 = 1B (hexa)
	// Note (22 + 5) = 27 (in decimal)
	$task->additionOfNumber(16, 5, 16);
	// Test B
	// Base : 4
	// 42 + 14 = 123 (Base 4)
	$task->additionOfNumber(42, 15, 4);
	// Test C
	// Base : 8
	// 453 + 234 = 707 (Base 8)
	$task->additionOfNumber(453, 234, 8);
	// Test C
	// Base : 3
	// (-45) + (-23) = -222 (Base 3)
	$task->additionOfNumber(-45, -23, 3);
}
main();

Output

 Given num1 : 16
 Given num2 : 5
 Given Base : 16
 Addition : 1B

 Given num1 : 42
 Given num2 : 15
 Given Base : 4
 Addition : 123

 Given num1 : 453
 Given num2 : 234
 Given Base : 8
 Addition : 707

 Given num1 : -45
 Given num2 : -23
 Given Base : 3
 Addition : -222
// Node JS program
// Add two integers of given base
class Addition
{
	charValue(num)
	{
		if (num >= 0 && num <= 9)
		{
			return String.fromCharCode((num + '0'.charCodeAt(0)));
		}
		else
		{
			return String.fromCharCode((num - 10 + 'A'.charCodeAt(0)));
		}
	}
	// Convert given number into decimal number
	convertToDecimal(num, baseValue)
	{
		if (baseValue == 10)
		{
			return num;
		}
		var n = num;
		if (n < 0)
		{
			n = -n;
		}
		var multiplier = 1;
		var result = 0;
		while (n != 0)
		{
			result += (n % 10) * multiplier;
			multiplier *= baseValue;
			n = parseInt(n / 10);
		}
		if (num < 0)
		{
			return -result;
		}
		return result;
	}
	additionOfNumber(num1, num2, baseValue)
	{
		// Convert Given numbers into decimal and addition of it value
		var sum = this.convertToDecimal(num1, baseValue) + 
                  this.convertToDecimal(num2, baseValue);
		var flag = false;
		if (sum < 0)
		{
			sum = -sum;
			flag = true;
		}
		// This is used to store result
		var result = "";
		// Transform sum to given base
		while (sum > 0)
		{
			result = (this.charValue(sum % baseValue)) + result;
			sum = parseInt(sum / baseValue);
		}
		if (flag == true)
		{
			result = "-" + result;
		}
		// Display given numbers
		process.stdout.write("\n Given num1 : " + num1);
		process.stdout.write("\n Given num2 : " + num2);
		process.stdout.write("\n Given Base : " + baseValue);
		// Display result
		process.stdout.write("\n Addition : " + result + "\n");
	}
}

function main()
{
	var task = new Addition();
	// Test A
	// Base : 16
	// 16 + 5 = 1B (hexa)
	// Note (22 + 5) = 27 (in decimal)
	task.additionOfNumber(16, 5, 16);
	// Test B
	// Base : 4
	// 42 + 14 = 123 (Base 4)
	task.additionOfNumber(42, 15, 4);
	// Test C
	// Base : 8
	// 453 + 234 = 707 (Base 8)
	task.additionOfNumber(453, 234, 8);
	// Test C
	// Base : 3
	// (-45) + (-23) = -222 (Base 3)
	task.additionOfNumber(-45, -23, 3);
}
main();

Output

 Given num1 : 16
 Given num2 : 5
 Given Base : 16
 Addition : 1B

 Given num1 : 42
 Given num2 : 15
 Given Base : 4
 Addition : 123

 Given num1 : 453
 Given num2 : 234
 Given Base : 8
 Addition : 707

 Given num1 : -45
 Given num2 : -23
 Given Base : 3
 Addition : -222
#  Python 3 program
#  Add two integers of given base
class Addition :
	def charValue(self, num) :
		if (num >= 0 and num <= 9) :
			return chr((num + ord('0')))
		else :
			return chr((num - 10 + ord('A')))
		
	
	#  Convert given number into decimal number
	def convertToDecimal(self, num, baseValue) :
		if (baseValue == 10) :
			return num
		
		n = num
		if (n < 0) :
			n = -n
		
		multiplier = 1
		result = 0
		while (n != 0) :
			result += (n % 10) * multiplier
			multiplier *= baseValue
			n = int(n / 10)
		
		if (num < 0) :
			return -result
		
		return result
	
	def additionOfNumber(self, num1, num2, baseValue) :
		#  Convert Given numbers into decimal and addition of it value
		sum = self.convertToDecimal(
          num1, baseValue
        ) + self.convertToDecimal(
          num2, baseValue
        )
		flag = False
		if (sum < 0) :
			sum = -sum
			flag = True
		
		#  This is used to store result
		result = ""
		#  Transform sum to given base
		while (sum > 0) :
			result = str(self.charValue(sum % baseValue)) + result
			sum = int(sum / baseValue)
		
		if (flag == True) :
			result = "-"+ result
		
		#  Display given numbers
		print("\n Given num1 : ", num1, end = "")
		print("\n Given num2 : ", num2, end = "")
		print("\n Given Base : ", baseValue, end = "")
		#  Display result
		print("\n Addition : ", result )
	

def main() :
	task = Addition()
	#  Test A
	#  Base : 16
	#  16 + 5 = 1B (hexa)
	#  Note (22 + 5) = 27 (in decimal)
	task.additionOfNumber(16, 5, 16)
	#  Test B
	#  Base : 4
	#  42 + 14 = 123 (Base 4)
	task.additionOfNumber(42, 15, 4)
	#  Test C
	#  Base : 8
	#  453 + 234 = 707 (Base 8)
	task.additionOfNumber(453, 234, 8)
	#  Test C
	#  Base : 3
	#  (-45) + (-23) = -222 (Base 3)
	task.additionOfNumber(-45, -23, 3)

if __name__ == "__main__": main()

Output

 Given num1 :  16
 Given num2 :  5
 Given Base :  16
 Addition :  1B

 Given num1 :  42
 Given num2 :  15
 Given Base :  4
 Addition :  123

 Given num1 :  453
 Given num2 :  234
 Given Base :  8
 Addition :  707

 Given num1 :  -45
 Given num2 :  -23
 Given Base :  3
 Addition :  -222
#  Ruby program
#  Add two integers of given base
class Addition 
	def charValue(num) 
		if (num >= 0 && num <= 9) 
			return ((num + '0'.ord)).chr
		else
			return ((num - 10 + 'A'.ord)).chr
		end

	end

	#  Convert given number into decimal number
	def convertToDecimal(num, baseValue) 
		if (baseValue == 10) 
			return num
		end

		n = num
		if (n < 0) 
			n = -n
		end

		multiplier = 1
		result = 0
		while (n != 0) 
			result += (n % 10) * multiplier
			multiplier *= baseValue
			n = n / 10
		end

		if (num < 0) 
			return -result
		end

		return result
	end

	def additionOfNumber(num1, num2, baseValue) 
		#  Convert Given numbers into decimal and addition of it value
		sum = self.convertToDecimal(num1, baseValue) + 
          self.convertToDecimal(num2, baseValue)
		flag = false
		if (sum < 0) 
			sum = -sum
			flag = true
		end

		#  This is used to store result
		result = ""
		#  Transform sum to given base
		while (sum > 0) 
			result = (self.charValue(sum % baseValue)).to_s + result
			sum = sum / baseValue
		end

		if (flag == true) 
			result = "-"+ result
		end

		#  Display given numbers
		print("\n Given num1 : ", num1)
		print("\n Given num2 : ", num2)
		print("\n Given Base : ", baseValue)
		#  Display result
		print("\n Addition : ", result ,"\n")
	end

end

def main() 
	task = Addition.new()
	#  Test A
	#  Base : 16
	#  16 + 5 = 1B (hexa)
	#  Note (22 + 5) = 27 (in decimal)
	task.additionOfNumber(16, 5, 16)
	#  Test B
	#  Base : 4
	#  42 + 14 = 123 (Base 4)
	task.additionOfNumber(42, 15, 4)
	#  Test C
	#  Base : 8
	#  453 + 234 = 707 (Base 8)
	task.additionOfNumber(453, 234, 8)
	#  Test C
	#  Base : 3
	#  (-45) + (-23) = -222 (Base 3)
	task.additionOfNumber(-45, -23, 3)
end

main()

Output

 Given num1 : 16
 Given num2 : 5
 Given Base : 16
 Addition : 1B

 Given num1 : 42
 Given num2 : 15
 Given Base : 4
 Addition : 123

 Given num1 : 453
 Given num2 : 234
 Given Base : 8
 Addition : 707

 Given num1 : -45
 Given num2 : -23
 Given Base : 3
 Addition : -222
// Scala program
// Add two integers of given base
class Addition()
{
	def charValue(num: Int): Char = {
		if (num >= 0 && num <= 9)
		{
			return (num + '0'.toInt).toChar;
		}
		else
		{
			return (num - 10 + 'A'.toInt).toChar;
		}
	}
	// Convert given number into decimal number
	def convertToDecimal(num: Int, baseValue: Int): Int = {
		if (baseValue == 10)
		{
			return num;
		}
		var n: Int = num;
		if (n < 0)
		{
			n = -n;
		}
		var multiplier: Int = 1;
		var result: Int = 0;
		while (n != 0)
		{
			result += (n % 10) * multiplier;
			multiplier *= baseValue;
			n = n / 10;
		}
		if (num < 0)
		{
			return -result;
		}
		return result;
	}
	def additionOfNumber(num1: Int, num2: Int, baseValue: Int): Unit = {
		// Convert Given numbers into decimal and addition of it value
		var sum: Int = convertToDecimal(num1, baseValue) + 
          convertToDecimal(num2, baseValue);
		var flag: Boolean = false;
		if (sum < 0)
		{
			sum = -sum;
			flag = true;
		}
		// This is used to store result
		var result: String = "";
		// Transform sum to given base
		while (sum > 0)
		{
			result = (charValue(sum % baseValue)).toString() + result;
			sum = sum / baseValue;
		}
		if (flag == true)
		{
			result = "-" + result;
		}
		// Display given numbers
		print("\n Given num1 : " + num1);
		print("\n Given num2 : " + num2);
		print("\n Given Base : " + baseValue);
		// Display result
		print("\n Addition : " + result + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Addition = new Addition();
		// Test A
		// Base : 16
		// 16 + 5 = 1B (hexa)
		// Note (22 + 5) = 27 (in decimal)
		task.additionOfNumber(16, 5, 16);
		// Test B
		// Base : 4
		// 42 + 14 = 123 (Base 4)
		task.additionOfNumber(42, 15, 4);
		// Test C
		// Base : 8
		// 453 + 234 = 707 (Base 8)
		task.additionOfNumber(453, 234, 8);
		// Test C
		// Base : 3
		// (-45) + (-23) = -222 (Base 3)
		task.additionOfNumber(-45, -23, 3);
	}
}

Output

 Given num1 : 16
 Given num2 : 5
 Given Base : 16
 Addition : 1B

 Given num1 : 42
 Given num2 : 15
 Given Base : 4
 Addition : 123

 Given num1 : 453
 Given num2 : 234
 Given Base : 8
 Addition : 707

 Given num1 : -45
 Given num2 : -23
 Given Base : 3
 Addition : -222
// Swift 4 program
// Add two integers of given base
class Addition
{
	func charValue(_ num: Int) -> Character
	{
		if (num >= 0 && num <= 9)
		{
			return Character(UnicodeScalar(num + 48)!);
		}
		else
		{
			return Character(UnicodeScalar(num - 10 + 65)!);
		}
	}
	// Convert given number into decimal number
	func convertToDecimal(_ num: Int, _ baseValue: Int) -> Int
	{
		if (baseValue == 10)
		{
			return num;
		}
		var n: Int = num;
		if (n < 0)
		{
			n = -n;
		}
		var multiplier: Int = 1;
		var result: Int = 0;
		while (n  != 0)
		{
			result += (n % 10) * multiplier;
			multiplier *= baseValue;
			n = n / 10;
		}
		if (num < 0)
		{
			return -result;
		}
		return result;
	}
	func additionOfNumber(_ num1: Int, _ num2: Int, _ baseValue: Int)
	{
		// Convert Given numbers into decimal and addition of it value
		var sum: Int = self.convertToDecimal(num1, baseValue) + 
          self.convertToDecimal(num2, baseValue);
		var flag: Bool = false;
		if (sum < 0)
		{
			sum = -sum;
			flag = true;
		}
		// This is used to store result
		var result: String = "";
		// Transform sum to given base
		while (sum > 0)
		{
			result = String((self.charValue(sum % baseValue))) + result;
			sum = sum / baseValue;
		}
		if (flag == true)
		{
			result = "-"
			+ result;
		}
		// Display given numbers
		print("\n Given num1 : ", num1, terminator: "");
		print("\n Given num2 : ", num2, terminator: "");
		print("\n Given Base : ", baseValue, terminator: "");
		// Display result
		print("\n Addition : ", result );
	}
}
func main()
{
	let task: Addition = Addition();
	// Test A
	// Base : 16
	// 16 + 5 = 1B (hexa)
	// Note (22 + 5) = 27 (in decimal)
	task.additionOfNumber(16, 5, 16);
	// Test B
	// Base : 4
	// 42 + 14 = 123 (Base 4)
	task.additionOfNumber(42, 15, 4);
	// Test C
	// Base : 8
	// 453 + 234 = 707 (Base 8)
	task.additionOfNumber(453, 234, 8);
	// Test C
	// Base : 3
	// (-45) + (-23) = -222 (Base 3)
	task.additionOfNumber(-45, -23, 3);
}
main();

Output

 Given num1 :  16
 Given num2 :  5
 Given Base :  16
 Addition :  1B

 Given num1 :  42
 Given num2 :  15
 Given Base :  4
 Addition :  123

 Given num1 :  453
 Given num2 :  234
 Given Base :  8
 Addition :  707

 Given num1 :  -45
 Given num2 :  -23
 Given Base :  3
 Addition :  -222
// Kotlin program
// Add two integers of given base
class Addition
{
	fun charValue(num: Int): Char
	{
		if (num >= 0 && num <= 9)
		{
			return (num + '0'.toInt()).toChar();
		}
		else
		{
			return (num - 10 + 'A'.toInt()).toChar();
		}
	}
	// Convert given number into decimal number
	fun convertToDecimal(num: Int, baseValue: Int): Int
	{
		if (baseValue == 10)
		{
			return num;
		}
		var n: Int = num;
		if (n < 0)
		{
			n = -n;
		}
		var multiplier: Int = 1;
		var result: Int = 0;
		while (n != 0)
		{
			result += (n % 10) * multiplier;
			multiplier *= baseValue;
			n = n / 10;
		}
		if (num < 0)
		{
			return -result;
		}
		return result;
	}
	fun additionOfNumber(num1: Int, num2: Int, baseValue: Int): Unit
	{
		// Convert Given numbers into decimal and addition of it value
		var sum: Int = this.convertToDecimal(num1, baseValue) +
          this.convertToDecimal(num2, baseValue);
		var flag: Boolean = false;
		if (sum < 0)
		{
			sum = -sum;
			flag = true;
		}
		// This is used to store result
		var result: String = "";
		// Transform sum to given base
		while (sum > 0)
		{
			result = (this.charValue(sum % baseValue)).toString() + result;
			sum = sum / baseValue;
		}
		if (flag == true)
		{
			result = "-" + result;
		}
		// Display given numbers
		print("\n Given num1 : " + num1);
		print("\n Given num2 : " + num2);
		print("\n Given Base : " + baseValue);
		// Display result
		print("\n Addition : " + result + "\n");
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Addition = Addition();
	// Test A
	// Base : 16
	// 16 + 5 = 1B (hexa)
	// Note (22 + 5) = 27 (in decimal)
	task.additionOfNumber(16, 5, 16);
	// Test B
	// Base : 4
	// 42 + 14 = 123 (Base 4)
	task.additionOfNumber(42, 15, 4);
	// Test C
	// Base : 8
	// 453 + 234 = 707 (Base 8)
	task.additionOfNumber(453, 234, 8);
	// Test C
	// Base : 3
	// (-45) + (-23) = -222 (Base 3)
	task.additionOfNumber(-45, -23, 3);
}

Output

 Given num1 : 16
 Given num2 : 5
 Given Base : 16
 Addition : 1B

 Given num1 : 42
 Given num2 : 15
 Given Base : 4
 Addition : 123

 Given num1 : 453
 Given num2 : 234
 Given Base : 8
 Addition : 707

 Given num1 : -45
 Given num2 : -23
 Given Base : 3
 Addition : -222


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