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 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 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
Above approach can be used to add numbers in any base. We don’t have to do string operations if 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.
- How to swap two numbers without using a temporary variable?
- Write a program to calculate pow(x,n)
- Write a program to reverse digits of a number
- Program to find whether a no is power of two
- Lucky Numbers
- Write an Efficient Method to Check if a Number is Multiple of 3
- Program to find parity
- Write an Efficient C Program to Reverse Bits of a Number
- Ugly Numbers
- Write a program to print all permutations of a given string
- Count of Binary Digit numbers smaller than N
- Write you own Power without using multiplication(*) and division(/) operators
- Program for Fibonacci numbers
- Average of a stream of numbers
- Add two numbers without using arithmetic operators
- To find sum of two numbers without using any operator
- Count numbers that don't contain 3
- Program to find amount of water in a given glass
- Program to convert a given number to words
- Efficient program to calculate e^x