Add two integers of given base
In mathematics and computer science, working with numbers of different bases (radices) is common. Each base represents a different numerical system. Adding two integers of a given base involves performing addition using the rules of that particular base. This task is useful in various computer science applications, such as cryptography, number representation, and encoding.
Problem Statement and Description
The problem is to add two integers that are given in a specific base and produce the sum in the same base. This involves converting the given numbers to decimal form, performing the addition, and then converting the result back to the original base. The algorithm needs to handle both positive and negative numbers as well as different bases.
Example
Let's consider an example where we want to add two numbers: 16
and 5
. These numbers are in
base 16
(hexadecimal). The algorithm should convert them to decimal, perform the addition
(22 + 5 = 27
in decimal), and then convert the result back to base 16
, which is
1B
.
Idea to Solve the Problem
To solve this problem, the algorithm performs the following steps:
- Define a
charValue
function to convert a digit into its character representation in the given base. - Define a
convertToDecimal
function to convert a number from the given base to decimal. - Define an
additionOfNumber
function to add two numbers in the given base. - Inside the
additionOfNumber
function, convert the given numbers to decimal using theconvertToDecimal
function. - Add the decimal numbers and store the result.
- Convert the result back to the given base using the
charValue
function and store it in a string. - If the result is negative, prepend a minus sign.
- Display the original numbers, the base, and the addition result.
Pseudocode
Here's the pseudocode for the algorithm:
function charValue(num):
if num is between 0 and 9:
return character representation of num
else:
return character representation of (num - 10 + 'A')
function convertToDecimal(num, baseValue):
if baseValue is 10:
return num
Convert num to positive if it's negative
Initialize multiplier to 1
Initialize result to 0
While num is not 0:
Add (num % 10) * multiplier to result
Multiply multiplier by baseValue
Divide num by 10
If num was negative, return -result
Else, return result
function additionOfNumber(num1, num2, baseValue):
Convert num1 and num2 to decimal using convertToDecimal
Calculate sum = num1 + num2
If sum is negative, set flag to true and make sum positive
Initialize result string to empty
Convert sum to baseValue and append to result
If flag is true, prepend a minus sign to result
Display original numbers, baseValue, and result
main:
Create an instance of the Addition class
Call additionOfNumber for test cases
Algorithm Explanation
- Define the
charValue
function to convert a digit into its character representation in the given base. - Define the
convertToDecimal
function to convert a number from the given base to decimal. - Define the
additionOfNumber
function to add two numbers in the given base. - In the
additionOfNumber
function, convert the numbers to decimal usingconvertToDecimal
. - Add the decimal numbers and store the result. Handle the flag for negative numbers.
- Convert the result back to the given base using
charValue
and store it in a string. - If the result is negative, prepend a minus sign.
- Display the original numbers, base, and addition result.
- In the
main
program, create an instance of theAddition
class and calladditionOfNumber
for the provided test cases.
Program Solution
// 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
Time Complexity
The time complexity of this algorithm mainly depends on the arithmetic operations and the GCD calculation used
within the convertToDecimal
function. Arithmetic operations and comparisons are typically constant time
operations, while the GCD calculation has a time complexity of O(log(min(a, b))). Therefore, the overall time
complexity of the algorithm is reasonable for most practical scenarios.
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