Addition of two octal numbers
Here given code implementation process.
/*
Java program
Addition of two octal numbers
*/
public class Addition
{
public int decimalValue(char x)
{
return x - '0';
}
// Accepting two octal numbers and perform addition
public void addOctalNumber(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 = "";
while (i >= 0 || j >= 0)
{
if (i >= 0 && j >= 0)
{
// When both digit number exists
temp = decimalValue(a.charAt(i)) +
decimalValue(b.charAt(j)) + carry;
i--;
j--;
}
else if (i >= 0)
{
// When digit exists in A
temp = decimalValue(a.charAt(i)) + carry;
i--;
}
else
{
// When digit exists in B
temp = decimalValue(b.charAt(j)) + carry;
j--;
}
result = (temp % 8) + result;
carry = temp / 8;
}
if (carry != 0)
{
result = 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 = "563"
// b = "123"
// Decimal
// 563 = (371)
// 4235 = (2205)
// -----------
// 5020 = 2576
task.addOctalNumber("563", "4235");
// Example B
// -----------
// a = "1130"
// b = "157"
// Decimal
// 1130 = 600
// 157 = 111
// -----------
// 1307 = 711
task.addOctalNumber("1130", "157");
}
}
Output
Given A : 563
Given B : 4235
Result : 5020
Given A : 1130
Given B : 157
Result : 1307
package main
import "strconv"
import "fmt"
/*
Go program
Addition of two octal numbers
*/
type Addition struct {}
func getAddition() * Addition {
var me *Addition = &Addition {}
return me
}
func(this Addition) decimalValue(x byte) int {
return int(x) - int('0')
}
// Accepting two octal numbers and perform addition
func(this Addition) addOctalNumber(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 = ""
for (i >= 0 || j >= 0) {
if i >= 0 && j >= 0 {
// When both digit number exists
temp = this.decimalValue(a[i]) + this.decimalValue(b[j]) + carry
i--
j--
} else if i >= 0 {
// When digit exists in A
temp = this.decimalValue(a[i]) + carry
i--
} else {
// When digit exists in B
temp = this.decimalValue(b[j]) + carry
j--
}
result = strconv.Itoa((temp % 8)) + result
carry = temp / 8
}
if carry != 0 {
result = strconv.Itoa(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 = "563"
// b = "123"
// Decimal
// 563 = (371)
// 4235 = (2205)
// -----------
// 5020 = 2576
task.addOctalNumber("563", "4235")
// Example B
// -----------
// a = "1130"
// b = "157"
// Decimal
// 1130 = 600
// 157 = 111
// -----------
// 1307 = 711
task.addOctalNumber("1130", "157")
}
Output
Given A : 563
Given B : 4235
Result : 5020
Given A : 1130
Given B : 157
Result : 1307
// Include header file
#include <iostream>
#include <string>
using namespace std;
/*
C++ program
Addition of two octal numbers
*/
class Addition
{
public: int decimalValue(char x)
{
return x - '0';
}
// Accepting two octal numbers and perform addition
void addOctalNumber(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 = "";
while (i >= 0 || j >= 0)
{
if (i >= 0 && j >= 0)
{
// When both digit number exists
temp = this->decimalValue(a[i]) +
this->decimalValue(b[j]) + carry;
i--;
j--;
}
else if (i >= 0)
{
// When digit exists in A
temp = this->decimalValue(a[i]) + carry;
i--;
}
else
{
// When digit exists in B
temp = this->decimalValue(b[j]) + carry;
j--;
}
result = to_string((temp % 8)) + result;
carry = temp / 8;
}
if (carry != 0)
{
result = to_string(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 = "563"
// b = "123"
// Decimal
// 563 = (371)
// 4235 = (2205)
// -----------
// 5020 = 2576
task->addOctalNumber("563", "4235");
// Example B
// -----------
// a = "1130"
// b = "157"
// Decimal
// 1130 = 600
// 157 = 111
// -----------
// 1307 = 711
task->addOctalNumber("1130", "157");
return 0;
}
Output
Given A : 563
Given B : 4235
Result : 5020
Given A : 1130
Given B : 157
Result : 1307
// Include namespace system
using System;
/*
Csharp program
Addition of two octal numbers
*/
public class Addition
{
public int decimalValue(char x)
{
return x - '0';
}
// Accepting two octal numbers and perform addition
public void addOctalNumber(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 = "";
while (i >= 0 || j >= 0)
{
if (i >= 0 && j >= 0)
{
// When both digit number exists
temp = this.decimalValue(a[i]) +
this.decimalValue(b[j]) + carry;
i--;
j--;
}
else if (i >= 0)
{
// When digit exists in A
temp = this.decimalValue(a[i]) + carry;
i--;
}
else
{
// When digit exists in B
temp = this.decimalValue(b[j]) + carry;
j--;
}
result = (temp % 8) + result;
carry = temp / 8;
}
if (carry != 0)
{
result = 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 = "563"
// b = "123"
// Decimal
// 563 = (371)
// 4235 = (2205)
// -----------
// 5020 = 2576
task.addOctalNumber("563", "4235");
// Example B
// -----------
// a = "1130"
// b = "157"
// Decimal
// 1130 = 600
// 157 = 111
// -----------
// 1307 = 711
task.addOctalNumber("1130", "157");
}
}
Output
Given A : 563
Given B : 4235
Result : 5020
Given A : 1130
Given B : 157
Result : 1307
<?php
/*
Php program
Addition of two octal numbers
*/
class Addition
{
public function decimalValue($x)
{
return ord($x) - ord('0');
}
// Accepting two octal numbers and perform addition
public function addOctalNumber($a, $b)
{
$n = strlen($a);
$m = strlen($b);
$i = $n - 1;
$j = $m - 1;
$temp = 0;
$carry = 0;
$result = "";
while ($i >= 0 || $j >= 0)
{
if ($i >= 0 && $j >= 0)
{
// When both digit number exists
$temp = $this->decimalValue($a[$i]) +
$this->decimalValue($b[$j]) + $carry;
$i--;
$j--;
}
else if ($i >= 0)
{
// When digit exists in A
$temp = $this->decimalValue($a[$i]) + $carry;
$i--;
}
else
{
// When digit exists in B
$temp = $this->decimalValue($b[$j]) + $carry;
$j--;
}
$result = strval(($temp % 8)).$result;
$carry = (int)($temp / 8);
}
if ($carry != 0)
{
$result = strval($carry).$result;
}
echo(" Given A : ".$a.
"\n");
echo(" Given B : ".$b.
"\n");
echo(" Result : ".$result.
"\n");
}
}
function main()
{
$task = new Addition();
// Example A
// -----------
// a = "563"
// b = "123"
// Decimal
// 563 = (371)
// 4235 = (2205)
// -----------
// 5020 = 2576
$task->addOctalNumber("563", "4235");
// Example B
// -----------
// a = "1130"
// b = "157"
// Decimal
// 1130 = 600
// 157 = 111
// -----------
// 1307 = 711
$task->addOctalNumber("1130", "157");
}
main();
Output
Given A : 563
Given B : 4235
Result : 5020
Given A : 1130
Given B : 157
Result : 1307
/*
Node JS program
Addition of two octal numbers
*/
class Addition
{
decimalValue(x)
{
return x.charCodeAt(0) - '0'.charCodeAt(0);
}
// Accepting two octal numbers and perform addition
addOctalNumber(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 = "";
while (i >= 0 || j >= 0)
{
if (i >= 0 && j >= 0)
{
// When both digit number exists
temp = this.decimalValue(a.charAt(i)) +
this.decimalValue(b.charAt(j)) + carry;
i--;
j--;
}
else if (i >= 0)
{
// When digit exists in A
temp = this.decimalValue(a.charAt(i)) + carry;
i--;
}
else
{
// When digit exists in B
temp = this.decimalValue(b.charAt(j)) + carry;
j--;
}
result = (temp % 8) + result;
carry = parseInt(temp / 8);
}
if (carry != 0)
{
result = 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 = "563"
// b = "123"
// Decimal
// 563 = (371)
// 4235 = (2205)
// -----------
// 5020 = 2576
task.addOctalNumber("563", "4235");
// Example B
// -----------
// a = "1130"
// b = "157"
// Decimal
// 1130 = 600
// 157 = 111
// -----------
// 1307 = 711
task.addOctalNumber("1130", "157");
}
main();
Output
Given A : 563
Given B : 4235
Result : 5020
Given A : 1130
Given B : 157
Result : 1307
# Python 3 program
# Addition of two octal numbers
class Addition :
def decimalValue(self, x) :
return ord(x) - ord('0')
# Accepting two octal numbers and perform addition
def addOctalNumber(self, a, b) :
n = len(a)
m = len(b)
i = n - 1
j = m - 1
temp = 0
carry = 0
result = ""
while (i >= 0 or j >= 0) :
if (i >= 0 and j >= 0) :
# When both digit number exists
temp = self.decimalValue(a[i]) + self.decimalValue(b[j]) + carry
i -= 1
j -= 1
elif (i >= 0) :
# When digit exists in A
temp = self.decimalValue(a[i]) + carry
i -= 1
else :
# When digit exists in B
temp = self.decimalValue(b[j]) + carry
j -= 1
result = str((temp % 8)) + result
carry = int(temp / 8)
if (carry != 0) :
result = str(carry) + result
print(" Given A : ", a)
print(" Given B : ", b)
print(" Result : ", result)
def main() :
task = Addition()
# Example A
# -----------
# a = "563"
# b = "123"
# Decimal
# 563 = (371)
# 4235 = (2205)
# -----------
# 5020 = 2576
task.addOctalNumber("563", "4235")
# Example B
# -----------
# a = "1130"
# b = "157"
# Decimal
# 1130 = 600
# 157 = 111
# -----------
# 1307 = 711
task.addOctalNumber("1130", "157")
if __name__ == "__main__": main()
Output
Given A : 563
Given B : 4235
Result : 5020
Given A : 1130
Given B : 157
Result : 1307
# Ruby program
# Addition of two octal numbers
class Addition
def decimalValue(x)
return x.ord - '0'.ord
end
# Accepting two octal numbers and perform addition
def addOctalNumber(a, b)
n = a.length
m = b.length
i = n - 1
j = m - 1
temp = 0
carry = 0
result = ""
while (i >= 0 || j >= 0)
if (i >= 0 && j >= 0)
# When both digit number exists
temp = self.decimalValue(a[i]) +
self.decimalValue(b[j]) + carry
i -= 1
j -= 1
elsif (i >= 0)
# When digit exists in A
temp = self.decimalValue(a[i]) + carry
i -= 1
else
# When digit exists in B
temp = self.decimalValue(b[j]) + carry
j -= 1
end
result = (temp % 8).to_s + result
carry = temp / 8
end
if (carry != 0)
result = 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 = "563"
# b = "123"
# Decimal
# 563 = (371)
# 4235 = (2205)
# -----------
# 5020 = 2576
task.addOctalNumber("563", "4235")
# Example B
# -----------
# a = "1130"
# b = "157"
# Decimal
# 1130 = 600
# 157 = 111
# -----------
# 1307 = 711
task.addOctalNumber("1130", "157")
end
main()
Output
Given A : 563
Given B : 4235
Result : 5020
Given A : 1130
Given B : 157
Result : 1307
import scala.collection.mutable._;
/*
Scala program
Addition of two octal numbers
*/
class Addition()
{
def decimalValue(x: Char): Int = {
return x.toInt - '0'.toInt;
}
// Accepting two octal numbers and perform addition
def addOctalNumber(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 = "";
while (i >= 0 || j >= 0)
{
if (i >= 0 && j >= 0)
{
// When both digit number exists
temp = decimalValue(a.charAt(i)) +
decimalValue(b.charAt(j)) + carry;
i -= 1;
j -= 1;
}
else if (i >= 0)
{
// When digit exists in A
temp = decimalValue(a.charAt(i)) + carry;
i -= 1;
}
else
{
// When digit exists in B
temp = decimalValue(b.charAt(j)) + carry;
j -= 1;
}
result = (temp % 8).toString() + result;
carry = temp / 8;
}
if (carry != 0)
{
result = 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 = "563"
// b = "123"
// Decimal
// 563 = (371)
// 4235 = (2205)
// -----------
// 5020 = 2576
task.addOctalNumber("563", "4235");
// Example B
// -----------
// a = "1130"
// b = "157"
// Decimal
// 1130 = 600
// 157 = 111
// -----------
// 1307 = 711
task.addOctalNumber("1130", "157");
}
}
Output
Given A : 563
Given B : 4235
Result : 5020
Given A : 1130
Given B : 157
Result : 1307
import Foundation;
/*
Swift 4 program
Addition of two octal numbers
*/
class Addition
{
func decimalValue(_ x: Character) -> Int
{
return Int(UnicodeScalar(String(x))!.value) -
Int(UnicodeScalar(String("0"))!.value);
}
// Accepting two octal numbers and perform addition
func addOctalNumber(_ 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 = "";
while (i >= 0 || j >= 0)
{
if (i >= 0 && j >= 0)
{
// When both digit number exists
temp = self.decimalValue(a[i]) +
self.decimalValue(b[j]) + carry;
i -= 1;
j -= 1;
}
else if (i >= 0)
{
// When digit exists in A
temp = self.decimalValue(a[i]) + carry;
i -= 1;
}
else
{
// When digit exists in B
temp = self.decimalValue(b[j]) + carry;
j -= 1;
}
result = String((temp % 8)) + result;
carry = temp / 8;
}
if (carry != 0)
{
result = String(carry) + result;
}
print(" Given A : ", num1);
print(" Given B : ", num2);
print(" Result : ", result);
}
}
func main()
{
let task: Addition = Addition();
// Example A
// -----------
// a = "563"
// b = "123"
// Decimal
// 563 = (371)
// 4235 = (2205)
// -----------
// 5020 = 2576
task.addOctalNumber("563", "4235");
// Example B
// -----------
// a = "1130"
// b = "157"
// Decimal
// 1130 = 600
// 157 = 111
// -----------
// 1307 = 711
task.addOctalNumber("1130", "157");
}
main();
Output
Given A : 563
Given B : 4235
Result : 5020
Given A : 1130
Given B : 157
Result : 1307
/*
Kotlin program
Addition of two octal numbers
*/
class Addition
{
fun decimalValue(x: Char): Int
{
return x.toInt() - '0'.toInt();
}
// Accepting two octal numbers and perform addition
fun addOctalNumber(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 = "";
while (i >= 0 || j >= 0)
{
if (i >= 0 && j >= 0)
{
// When both digit number exists
temp = this.decimalValue(a.get(i)) +
this.decimalValue(b.get(j)) + carry;
i -= 1;
j -= 1;
}
else if (i >= 0)
{
// When digit exists in A
temp = this.decimalValue(a.get(i)) + carry;
i -= 1;
}
else
{
// When digit exists in B
temp = this.decimalValue(b.get(j)) + carry;
j -= 1;
}
result = (temp % 8).toString() + result;
carry = temp / 8;
}
if (carry != 0)
{
result = 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 = "563"
// b = "123"
// Decimal
// 563 = (371)
// 4235 = (2205)
// -----------
// 5020 = 2576
task.addOctalNumber("563", "4235");
// Example B
// -----------
// a = "1130"
// b = "157"
// Decimal
// 1130 = 600
// 157 = 111
// -----------
// 1307 = 711
task.addOctalNumber("1130", "157");
}
Output
Given A : 563
Given B : 4235
Result : 5020
Given A : 1130
Given B : 157
Result : 1307
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