hexa decimal number addition in java

input 2 hexadecimal numbers and then add them and print the result without any conversion to decimal .we have to code as we simply add them in reality

It can be done in O(n) approach where n is number of digits in hexadecimal number…

One of the way is using if else statements and adding them…
I can explain you in a bit detail If u tell me about input format and the constraint for number of digits…

moreover tell me If you are using abcdef or ABCDEF or it can be anything from both of them…

import java.math.BigInteger;

class BI
{
	public static void main (String[] args) throws java.lang.Exception
	{
	    BigInteger bi1 = new BigInteger("aBc", 16);
	    BigInteger bi2 = new BigInteger("DeF", 16);
	    BigInteger bi3 = bi1.add(bi2);
	    System.out.println(bi3.toString(16));
	}
}

Why settle for hexa decimals when you can perform calculations on a 36-base number??

so do you think Biginteger class won’t convert them in any other base ?? cuz he isn’t allowed to change the base…

Yes, you can always code the whole process yourself. I just tried to point out that there is already a very useful Java library class to take care of hexadecimals and other number systems.

1 Like

Okay that’s nice…

Thanks for answering