Addition of two hexadecimal numbers

Here given code implementation process.

/*
    Java program
    Addition of two hexadecimal numbers
*/
public class Addition
{
    public int decimalValue(char x)
    {
        if(x=='A' || x == 'a')
        {
            return 10;
        }
        if(x == 'B' || x == 'b')
        {
            return 11;
        }
        if(x == 'C' || x == 'c')
        {
            return 12;
        }
        if(x == 'D' || x == 'd')
        {
            return 13;
        }
        if(x == 'E' || x == 'e')
        {
            return 14;
        }
        if(x == 'F' || x == 'f')
        {
            return 15;
        }

        return  x - '0';
    }
    // Accepting two hexadecimal numbers and perform addition
    public void addHexadecimal(String a, String b)
    {
        int n = a.length();
        int m = b.length();
        int i = n-1;
        int j = m-1;
        int temp = 0;
        int carry = 0;
        String result = "";

        char[] hexaValue = {'0','1','2','3','4','5','6',
                            '7','8','9','A','B','C','D','E'};

        while(i >= 0 || j >= 0)
        {
            if(i >= 0 && j >= 0)
            {
                  
                temp = decimalValue(a.charAt(i)) + 
                  decimalValue(b.charAt(j)) + carry;

                i--;
                j--;
            }
            else if(i >= 0)
            {
                temp = decimalValue(a.charAt(i)) +  carry;
                i--;
            }
            else
            {
                temp = decimalValue(b.charAt(j)) + carry;
                j--;
            }

            result = hexaValue[(temp % 16)] + result;

            carry = temp / 16;
        }

        if(carry!=0)
        {
            result = hexaValue[carry] + result;
        }

		System.out.println(" Given A : " + a);
		System.out.println(" Given B : " + b);
		System.out.println(" Result  : " + result);


    }
    public static void main(String[] args)
    {
       Addition task = new Addition();
       // Example A
       // -----------
       // a = "3F"
       // b = "6D"
       // 
       // 3F = 63
       // 6D = 109
       // -----------
       // AC = 172    

       task.addHexadecimal("3F","6D");

       // Example B
       // -----------
       // a = "FD57"
       // b = "3e7"
       // 
       // FD57 = 64855
       // 3e7  = 999
       // -----------
       // 1013E = 65854     

       task.addHexadecimal("FD57","3e7");
    }
}

Output

 Given A : 3F
 Given B : 6D
 Result  : AC
 Given A : FD57
 Given B : 3e7
 Result  : 1013E
// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
    C++ program
    Addition of two hexadecimal numbers
*/
class Addition
{
	public: int decimalValue(char x)
	{
		if (x == 'A' || x == 'a')
		{
			return 10;
		}
		if (x == 'B' || x == 'b')
		{
			return 11;
		}
		if (x == 'C' || x == 'c')
		{
			return 12;
		}
		if (x == 'D' || x == 'd')
		{
			return 13;
		}
		if (x == 'E' || x == 'e')
		{
			return 14;
		}
		if (x == 'F' || x == 'f')
		{
			return 15;
		}
		return x - '0';
	}
	// Accepting two hexadecimal numbers and perform addition
	void addHexadecimal(string a, string b)
	{
		int n = a.length();
		int m = b.length();
		int i = n - 1;
		int j = m - 1;
		int temp = 0;
		int carry = 0;
		string result = "";
		char hexaValue[] = {
			'0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , 
            '8' , '9' , 'A' , 'B' , 'C' , 'D' , 'E'
		};
		while (i >= 0 || j >= 0)
		{
			if (i >= 0 && j >= 0)
			{
				temp = this->decimalValue(a[i]) + 
                  this->decimalValue(b[j]) + carry;
				i--;
				j--;
			}
			else if (i >= 0)
			{
				temp = this->decimalValue(a[i]) + carry;
				i--;
			}
			else
			{
				temp = this->decimalValue(b[j]) + carry;
				j--;
			}
			result = (hexaValue[(temp % 16)])  +  result;
			carry = temp / 16;
		}
		if (carry != 0)
		{
			result = (hexaValue[carry])  +  result;
		}
		cout << " Given A : " << a << endl;
		cout << " Given B : " << b << endl;
		cout << " Result  : " << result << endl;
	}
};
int main()
{
	Addition *task = new Addition();
	// Example A
	// -----------
	// a = "3F"
	// b = "6D"
	// 
	// 3F = 63
	// 6D = 109
	// -----------
	// AC = 172    
	task->addHexadecimal("3F", "6D");
	// Example B
	// -----------
	// a = "FD57"
	// b = "3e7"
	// 
	// FD57 = 64855
	// 3e7  = 999
	// -----------
	// 1013E = 65854     
	task->addHexadecimal("FD57", "3e7");
	return 0;
}

Output

 Given A : 3F
 Given B : 6D
 Result  : AC
 Given A : FD57
 Given B : 3e7
 Result  : 1013E
package main
import "fmt"
/*
    Go program
    Addition of two hexadecimal numbers
*/
type Addition struct {}
func getAddition() * Addition {
	var me *Addition = &Addition {}
	return me
}
func(this Addition) decimalValue(x byte) int {
	if x == 'A' || x == 'a' {
		return 10
	}
	if x == 'B' || x == 'b' {
		return 11
	}
	if x == 'C' || x == 'c' {
		return 12
	}
	if x == 'D' || x == 'd' {
		return 13
	}
	if x == 'E' || x == 'e' {
		return 14
	}
	if x == 'F' || x == 'f' {
		return 15
	}
	return int(x) - int('0')
}
// Accepting two hexadecimal numbers and perform addition
func(this Addition) addHexadecimal(a, b string) {
	var n int = len(a)
	var m int = len(b)
	var i int = n - 1
	var j int = m - 1
	var temp int = 0
	var carry int = 0
	var result string = ""
	var hexaValue = [] byte {
		'0',
		'1',
		'2',
		'3',
		'4',
		'5',
		'6',
		'7',
		'8',
		'9',
		'A',
		'B',
		'C',
		'D',
		'E',
	}
	for (i >= 0 || j >= 0) {
		if i >= 0 && j >= 0 {
			temp = this.decimalValue(a[i]) + this.decimalValue(b[j]) + carry
			i--
			j--
		} else if i >= 0 {
			temp = this.decimalValue(a[i]) + carry
			i--
		} else {
			temp = this.decimalValue(b[j]) + carry
			j--
		}
		result = string(hexaValue[(temp % 16)]) + result
		carry = temp / 16
	}
	if carry != 0 {
		result = string(hexaValue[carry]) + result
	}
	fmt.Println(" Given A : ", a)
	fmt.Println(" Given B : ", b)
	fmt.Println(" Result  : ", result)
}
func main() {
	var task * Addition = getAddition()
	// Example A
	// -----------
	// a = "3F"
	// b = "6D"
	// 
	// 3F = 63
	// 6D = 109
	// -----------
	// AC = 172    
	task.addHexadecimal("3F", "6D")
	// Example B
	// -----------
	// a = "FD57"
	// b = "3e7"
	// 
	// FD57 = 64855
	// 3e7  = 999
	// -----------
	// 1013E = 65854     
	task.addHexadecimal("FD57", "3e7")
}

Output

 Given A : 3F
 Given B : 6D
 Result  : AC
 Given A : FD57
 Given B : 3e7
 Result  : 1013E
// Include namespace system
using System;
/*
    Csharp program
    Addition of two hexadecimal numbers
*/
public class Addition
{
	public int decimalValue(char x)
	{
		if (x == 'A' || x == 'a')
		{
			return 10;
		}
		if (x == 'B' || x == 'b')
		{
			return 11;
		}
		if (x == 'C' || x == 'c')
		{
			return 12;
		}
		if (x == 'D' || x == 'd')
		{
			return 13;
		}
		if (x == 'E' || x == 'e')
		{
			return 14;
		}
		if (x == 'F' || x == 'f')
		{
			return 15;
		}
		return x - '0';
	}
	// Accepting two hexadecimal numbers and perform addition
	public void addHexadecimal(String a, String b)
	{
		int n = a.Length;
		int m = b.Length;
		int i = n - 1;
		int j = m - 1;
		int temp = 0;
		int carry = 0;
		String result = "";
		char[] hexaValue = {
			'0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' ,
           '9' , 'A' , 'B' , 'C' , 'D' , 'E'
		};
		while (i >= 0 || j >= 0)
		{
			if (i >= 0 && j >= 0)
			{
				temp = this.decimalValue(a[i]) + 
                  this.decimalValue(b[j]) + carry;
				i--;
				j--;
			}
			else if (i >= 0)
			{
				temp = this.decimalValue(a[i]) + carry;
				i--;
			}
			else
			{
				temp = this.decimalValue(b[j]) + carry;
				j--;
			}
			result = hexaValue[(temp % 16)] + result;
			carry = temp / 16;
		}
		if (carry != 0)
		{
			result = hexaValue[carry] + result;
		}
		Console.WriteLine(" Given A : " + a);
		Console.WriteLine(" Given B : " + b);
		Console.WriteLine(" Result  : " + result);
	}
	public static void Main(String[] args)
	{
		Addition task = new Addition();
		// Example A
		// -----------
		// a = "3F"
		// b = "6D"
		// 
		// 3F = 63
		// 6D = 109
		// -----------
		// AC = 172    
		task.addHexadecimal("3F", "6D");
		// Example B
		// -----------
		// a = "FD57"
		// b = "3e7"
		// 
		// FD57 = 64855
		// 3e7  = 999
		// -----------
		// 1013E = 65854     
		task.addHexadecimal("FD57", "3e7");
	}
}

Output

 Given A : 3F
 Given B : 6D
 Result  : AC
 Given A : FD57
 Given B : 3e7
 Result  : 1013E
<?php
/*
    Php program
    Addition of two hexadecimal numbers
*/
class Addition
{
	public	function decimalValue($x)
	{
		if ($x == 'A' || $x == 'a')
		{
			return 10;
		}
		if ($x == 'B' || $x == 'b')
		{
			return 11;
		}
		if ($x == 'C' || $x == 'c')
		{
			return 12;
		}
		if ($x == 'D' || $x == 'd')
		{
			return 13;
		}
		if ($x == 'E' || $x == 'e')
		{
			return 14;
		}
		if ($x == 'F' || $x == 'f')
		{
			return 15;
		}
		return ord($x) - ord('0');
	}
	// Accepting two hexadecimal numbers and perform addition
	public	function addHexadecimal($a, $b)
	{
		$n = strlen($a);
		$m = strlen($b);
		$i = $n - 1;
		$j = $m - 1;
		$temp = 0;
		$carry = 0;
		$result = "";
		$hexaValue = array('0', '1', '2', '3', '4', '5', '6', 
                           '7', '8', '9', 'A', 'B', 'C', 'D', 'E');
		while ($i >= 0 || $j >= 0)
		{
			if ($i >= 0 && $j >= 0)
			{
				$temp = $this->decimalValue($a[$i]) + 
                  $this->decimalValue($b[$j]) + $carry;
				$i--;
				$j--;
			}
			else if ($i >= 0)
			{
				$temp = $this->decimalValue($a[$i]) + $carry;
				$i--;
			}
			else
			{
				$temp = $this->decimalValue($b[$j]) + $carry;
				$j--;
			}
			$result = strval($hexaValue[($temp % 16)]).$result;
			$carry = (int)($temp / 16);
		}
		if ($carry != 0)
		{
			$result = strval($hexaValue[$carry]).$result;
		}
		echo(" Given A : ".$a.
			"\n");
		echo(" Given B : ".$b.
			"\n");
		echo(" Result  : ".$result.
			"\n");
	}
}

function main()
{
	$task = new Addition();
	// Example A
	// -----------
	// a = "3F"
	// b = "6D"
	// 
	// 3F = 63
	// 6D = 109
	// -----------
	// AC = 172    
	$task->addHexadecimal("3F", "6D");
	// Example B
	// -----------
	// a = "FD57"
	// b = "3e7"
	// 
	// FD57 = 64855
	// 3e7  = 999
	// -----------
	// 1013E = 65854     
	$task->addHexadecimal("FD57", "3e7");
}
main();

Output

 Given A : 3F
 Given B : 6D
 Result  : AC
 Given A : FD57
 Given B : 3e7
 Result  : 1013E
/*
    Node JS program
    Addition of two hexadecimal numbers
*/
class Addition
{
	decimalValue(x)
	{
		if (x == 'A' || x == 'a')
		{
			return 10;
		}
		if (x == 'B' || x == 'b')
		{
			return 11;
		}
		if (x == 'C' || x == 'c')
		{
			return 12;
		}
		if (x == 'D' || x == 'd')
		{
			return 13;
		}
		if (x == 'E' || x == 'e')
		{
			return 14;
		}
		if (x == 'F' || x == 'f')
		{
			return 15;
		}
		return x.charCodeAt(0) - '0'.charCodeAt(0);
	}
	// Accepting two hexadecimal numbers and perform addition
	addHexadecimal(a, b)
	{
		var n = a.length;
		var m = b.length;
		var i = n - 1;
		var j = m - 1;
		var temp = 0;
		var carry = 0;
		var result = "";
		var hexaValue = ['0', '1', '2', '3', '4', '5', '6', '7', 
                         '8', '9', 'A', 'B', 'C', 'D', 'E'];
		while (i >= 0 || j >= 0)
		{
			if (i >= 0 && j >= 0)
			{
				temp = this.decimalValue(a.charAt(i)) + 
                  this.decimalValue(b.charAt(j)) + carry;
				i--;
				j--;
			}
			else if (i >= 0)
			{
				temp = this.decimalValue(a.charAt(i)) + carry;
				i--;
			}
			else
			{
				temp = this.decimalValue(b.charAt(j)) + carry;
				j--;
			}
			result = hexaValue[(temp % 16)] + result;
			carry = parseInt(temp / 16);
		}
		if (carry != 0)
		{
			result = hexaValue[carry] + result;
		}
		console.log(" Given A : " + a);
		console.log(" Given B : " + b);
		console.log(" Result  : " + result);
	}
}

function main()
{
	var task = new Addition();
	// Example A
	// -----------
	// a = "3F"
	// b = "6D"
	// 
	// 3F = 63
	// 6D = 109
	// -----------
	// AC = 172    
	task.addHexadecimal("3F", "6D");
	// Example B
	// -----------
	// a = "FD57"
	// b = "3e7"
	// 
	// FD57 = 64855
	// 3e7  = 999
	// -----------
	// 1013E = 65854     
	task.addHexadecimal("FD57", "3e7");
}
main();

Output

 Given A : 3F
 Given B : 6D
 Result  : AC
 Given A : FD57
 Given B : 3e7
 Result  : 1013E
#    Python 3 program
#    Addition of two hexadecimal numbers
class Addition :
	def decimalValue(self, x) :
		if (x == 'A'
			or x == 'a') :
			return 10
		
		if (x == 'B'
			or x == 'b') :
			return 11
		
		if (x == 'C'
			or x == 'c') :
			return 12
		
		if (x == 'D'
			or x == 'd') :
			return 13
		
		if (x == 'E'
			or x == 'e') :
			return 14
		
		if (x == 'F'
			or x == 'f') :
			return 15
		
		return ord(x) - ord('0')
	
	#  Accepting two hexadecimal numbers and perform addition
	def addHexadecimal(self, a, b) :
		n = len(a)
		m = len(b)
		i = n - 1
		j = m - 1
		temp = 0
		carry = 0
		result = ""
		hexaValue = ['0', '1', '2', '3', '4', '5', 
                     '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E']
		while (i >= 0 or j >= 0) :
			if (i >= 0 and j >= 0) :
				temp = self.decimalValue(a[i]) + self.decimalValue(b[j]) + carry
				i -= 1
				j -= 1
			elif (i >= 0) :
				temp = self.decimalValue(a[i]) + carry
				i -= 1
			else :
				temp = self.decimalValue(b[j]) + carry
				j -= 1
			
			result = str(hexaValue[(temp % 16)]) + result
			carry = int(temp / 16)
		
		if (carry != 0) :
			result = str(hexaValue[carry]) + result
		
		print(" Given A : ", a)
		print(" Given B : ", b)
		print(" Result  : ", result)
	

def main() :
	task = Addition()
	#  Example A
	#  -----------
	#  a = "3F"
	#  b = "6D"
	#  3F = 63
	#  6D = 109
	#  -----------
	#  AC = 172    
	task.addHexadecimal("3F", "6D")
	#  Example B
	#  -----------
	#  a = "FD57"
	#  b = "3e7"
	#  FD57 = 64855
	#  3e7  = 999
	#  -----------
	#  1013E = 65854     
	task.addHexadecimal("FD57", "3e7")

if __name__ == "__main__": main()

Output

 Given A :  3F
 Given B :  6D
 Result  :  AC
 Given A :  FD57
 Given B :  3e7
 Result  :  1013E
#    Ruby program
#    Addition of two hexadecimal numbers
class Addition 
	def decimalValue(x) 
		if (x == 'A' || x == 'a') 
			return 10
		end

		if (x == 'B' || x == 'b') 
			return 11
		end

		if (x == 'C' || x == 'c') 
			return 12
		end

		if (x == 'D' || x == 'd') 
			return 13
		end

		if (x == 'E' || x == 'e') 
			return 14
		end

		if (x == 'F' || x == 'f') 
			return 15
		end

		return x.ord - '0'.ord
	end

	#  Accepting two hexadecimal numbers and perform addition
	def addHexadecimal(a, b) 
		n = a.length
		m = b.length
		i = n - 1
		j = m - 1
		temp = 0
		carry = 0
		result = ""
		hexaValue = ['0', '1', '2', '3', '4', '5', '6', '7', 
                     '8', '9', 'A', 'B', 'C', 'D', 'E']
		while (i >= 0 || j >= 0) 
			if (i >= 0 && j >= 0) 
				temp = self.decimalValue(a[i]) + 
                  self.decimalValue(b[j]) + carry
				i -= 1
				j -= 1
			elsif (i >= 0) 
				temp = self.decimalValue(a[i]) + carry
				i -= 1
			else
 
				temp = self.decimalValue(b[j]) + carry
				j -= 1
			end

			result = hexaValue[(temp % 16)].to_s + result
			carry = temp / 16
		end

		if (carry != 0) 
			result = hexaValue[carry].to_s + result
		end

		print(" Given A : ", a, "\n")
		print(" Given B : ", b, "\n")
		print(" Result  : ", result, "\n")
	end

end

def main() 
	task = Addition.new()
	#  Example A
	#  -----------
	#  a = "3F"
	#  b = "6D"
	#  3F = 63
	#  6D = 109
	#  -----------
	#  AC = 172    
	task.addHexadecimal("3F", "6D")
	#  Example B
	#  -----------
	#  a = "FD57"
	#  b = "3e7"
	#  FD57 = 64855
	#  3e7  = 999
	#  -----------
	#  1013E = 65854     
	task.addHexadecimal("FD57", "3e7")
end

main()

Output

 Given A : 3F
 Given B : 6D
 Result  : AC
 Given A : FD57
 Given B : 3e7
 Result  : 1013E
import scala.collection.mutable._;
/*
    Scala program
    Addition of two hexadecimal numbers
*/
class Addition()
{
	def decimalValue(x: Char): Int = {
		if (x == 'A' || x == 'a')
		{
			return 10;
		}
		if (x == 'B' || x == 'b')
		{
			return 11;
		}
		if (x == 'C' || x == 'c')
		{
			return 12;
		}
		if (x == 'D' || x == 'd')
		{
			return 13;
		}
		if (x == 'E' || x == 'e')
		{
			return 14;
		}
		if (x == 'F' || x == 'f')
		{
			return 15;
		}
		return x.toInt - '0'.toInt;
	}
	// Accepting two hexadecimal numbers and perform addition
	def addHexadecimal(a: String, b: String): Unit = {
		var n: Int = a.length();
		var m: Int = b.length();
		var i: Int = n - 1;
		var j: Int = m - 1;
		var temp: Int = 0;
		var carry: Int = 0;
		var result: String = "";
		var hexaValue: Array[Char] = Array('0', '1', '2', '3', '4', '5', 
                                           '6', '7', '8', '9', 'A', 'B', 
                                           'C', 'D', 'E');
		while (i >= 0 || j >= 0)
		{
			if (i >= 0 && j >= 0)
			{
				temp = decimalValue(a.charAt(i)) + 
                  decimalValue(b.charAt(j)) + carry;
				i -= 1;
				j -= 1;
			}
			else if (i >= 0)
			{
				temp = decimalValue(a.charAt(i)) + carry;
				i -= 1;
			}
			else
			{
				temp = decimalValue(b.charAt(j)) + carry;
				j -= 1;
			}
			result = hexaValue((temp % 16)).toString() + result;
			carry = temp / 16;
		}
		if (carry != 0)
		{
			result = hexaValue(carry).toString() + result;
		}
		println(" Given A : " + a);
		println(" Given B : " + b);
		println(" Result  : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Addition = new Addition();
		// Example A
		// -----------
		// a = "3F"
		// b = "6D"
		// 
		// 3F = 63
		// 6D = 109
		// -----------
		// AC = 172    
		task.addHexadecimal("3F", "6D");
		// Example B
		// -----------
		// a = "FD57"
		// b = "3e7"
		// 
		// FD57 = 64855
		// 3e7  = 999
		// -----------
		// 1013E = 65854     
		task.addHexadecimal("FD57", "3e7");
	}
}

Output

 Given A : 3F
 Given B : 6D
 Result  : AC
 Given A : FD57
 Given B : 3e7
 Result  : 1013E
import Foundation;
/*
    Swift 4 program
    Addition of two hexadecimal numbers
*/
class Addition
{
	func decimalValue(_ x: Character) -> Int
	{
		if (x == "A" || x == "a")
		{
			return 10;
		}
		if (x == "B" || x == "b")
		{
			return 11;
		}
		if (x == "C" || x == "c")
		{
			return 12;
		}
		if (x == "D" || x == "d")
		{
			return 13;
		}
		if (x == "E" || x == "e")
		{
			return 14;
		}
		if (x == "F" || x == "f")
		{
			return 15;
		}
		return Int(UnicodeScalar(String(x))!.value) - 
          Int(UnicodeScalar(String("0"))!.value);
	}
	// Accepting two hexadecimal numbers and perform addition
	func addHexadecimal(_ num1: String, _ num2: String)
	{
      	let a = Array(num1);
      	let b = Array(num2);      
		let n: Int = a.count;
		let m: Int = b.count;
		var i: Int = n - 1;
		var j: Int = m - 1;
		var temp: Int = 0;
		var carry: Int = 0;
		var result: String = "";
		let hexaValue: [Character] = ["0", "1", "2", "3", "4", 
                                      "5", "6", "7", "8", "9", "A", 
                                      "B", "C", "D", "E"];
		while (i >= 0 || j >= 0)
		{
			if (i >= 0 && j >= 0)
			{
				temp = self.decimalValue(a[i]) + 
                  self.decimalValue(b[j]) + carry;
				i -= 1;
				j -= 1;
			}
			else if (i >= 0)
			{
				temp = self.decimalValue(a[i]) + carry;
				i -= 1;
			}
			else
			{
				temp = self.decimalValue(b[j]) + carry;
				j -= 1;
			}
			result = String(hexaValue[(temp % 16)]) + result;
			carry = temp / 16;
		}
		if (carry  != 0)
		{
			result = String(hexaValue[carry]) + result;
		}
		print(" Given A : ", num1);
		print(" Given B : ", num2);
		print(" Result  : ", result);
	}
}
func main()
{
	let task: Addition = Addition();
	// Example A
	// -----------
	// a = "3F"
	// b = "6D"
	// 
	// 3F = 63
	// 6D = 109
	// -----------
	// AC = 172    
	task.addHexadecimal("3F", "6D");
	// Example B
	// -----------
	// a = "FD57"
	// b = "3e7"
	// 
	// FD57 = 64855
	// 3e7  = 999
	// -----------
	// 1013E = 65854     
	task.addHexadecimal("FD57", "3e7");
}
main();

Output

 Given A :  3F
 Given B :  6D
 Result  :  AC
 Given A :  FD57
 Given B :  3e7
 Result  :  1013E
/*
    Kotlin program
    Addition of two hexadecimal numbers
*/
class Addition
{
	fun decimalValue(x: Char): Int
	{
		if (x == 'A' || x == 'a')
		{
			return 10;
		}
		if (x == 'B' || x == 'b')
		{
			return 11;
		}
		if (x == 'C' || x == 'c')
		{
			return 12;
		}
		if (x == 'D' || x == 'd')
		{
			return 13;
		}
		if (x == 'E' || x == 'e')
		{
			return 14;
		}
		if (x == 'F' || x == 'f')
		{
			return 15;
		}
		return x.toInt() - '0'.toInt();
	}
	// Accepting two hexadecimal numbers and perform addition
	fun addHexadecimal(a: String, b: String): Unit
	{
		val n: Int = a.length;
		val m: Int = b.length;
		var i: Int = n - 1;
		var j: Int = m - 1;
		var temp: Int;
		var carry: Int = 0;
		var result: String = "";
		val hexaValue: Array < Char > = arrayOf(
          '0', '1', '2', '3', '4', '5', '6', '7', '8', 
          '9', 'A', 'B', 'C', 'D', 'E');
		while (i >= 0 || j >= 0)
		{
			if (i >= 0 && j >= 0)
			{
				temp = this.decimalValue(a.get(i)) + 
                  this.decimalValue(b.get(j)) + carry;
				i -= 1;
				j -= 1;
			}
			else if (i >= 0)
			{
				temp = this.decimalValue(a.get(i)) + carry;
				i -= 1;
			}
			else
			{
				temp = this.decimalValue(b.get(j)) + carry;
				j -= 1;
			}
			result = hexaValue[(temp % 16)].toString() + result;
			carry = temp / 16;
		}
		if (carry != 0)
		{
			result = hexaValue[carry].toString() + result;
		}
		println(" Given A : " + a);
		println(" Given B : " + b);
		println(" Result  : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Addition = Addition();
	// Example A
	// -----------
	// a = "3F"
	// b = "6D"
	// 
	// 3F = 63
	// 6D = 109
	// -----------
	// AC = 172    
	task.addHexadecimal("3F", "6D");
	// Example B
	// -----------
	// a = "FD57"
	// b = "3e7"
	// 
	// FD57 = 64855
	// 3e7  = 999
	// -----------
	// 1013E = 65854     
	task.addHexadecimal("FD57", "3e7");
}

Output

 Given A : 3F
 Given B : 6D
 Result  : AC
 Given A : FD57
 Given B : 3e7
 Result  : 1013E


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