Asked by Anshya.
Below are the different ways to add base 14 numbers.
Thanks to Raj for suggesting this method.
1. Convert both i/p base 14 numbers to base 10. 2. Add numbers. 3. Convert the result back to base 14.
Just add the numbers in base 14 in the same way we add in base 10. Add numerals of both numbers one by one from right to left. If there is a carry while adding two numerals, consider the carry for adding the next numerals.
Let us consider the presentation of base 14 numbers same as hexadecimal numbers
A --> 10 B --> 11 C --> 12 D --> 13
Example: num1 = 1 2 A num2 = C D 3 1. Add A and 3, we get 13(D). Since 13 is smaller than 14, carry becomes 0 and resultant numeral becomes D 2. Add 2, D and carry(0). we get 15. Since 15 is greater than 13, carry becomes 1 and resultant numeral is 15 - 14 = 1 3. Add 1, C and carry(1). we get 14. Since 14 is greater than 13, carry becomes 1 and resultant numeral is 14 - 14 = 0 Finally, there is a carry, so 1 is added as leftmost numeral and the result becomes 101D
Implementation of Method 2
Result is 1085
The above approach can be used to add numbers to any base. We don’t have to do string operations if the base is smaller than 10.
You can try extending the above program for numbers of different lengths.
Please comment if you find any bug in the program or a better approach to do the same.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Add two integers of different base and represent sum in smaller base of the two
- Given a number N in decimal base, find number of its digits in any base (base b)
- C++ program to find all numbers less than n, which are palindromic in base 10 and base 2.
- Program to add two integers of given base
- Find the Nth digit from right in base B of the given number in Decimal base
- Given a number N in decimal base, find the sum of digits in any base B
- Count of N-digit numbers in base K with no two consecutive zeroes
- All possible numbers of N digits and base B without leading zeros
- Largest Even and Odd N-digit numbers of base B
- Ternary number system or Base 3 numbers
- Find the total count of numbers up to N digits in a given base B
- Program to find the last digit of X in base Y
- Program to compute log a to any base b (logb a)
- Add two numbers without using arithmetic operators
- Add two numbers using ++ and/or --
- Count the number of carry operations required to add two numbers
- Add two numbers represented by Stacks
- How to add two Hexadecimal numbers
- 8 different ways to Add Two Numbers in C/C++
- Ways to write N as sum of two or more positive integers | Set-2