# Addition of two octal numbers

Here given code implementation process.

``````/*
Java program
*/
{
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)
{
// Example A
// -----------
// a = "563"
// b = "123"
//       Decimal
//  563 = (371)
// 4235 = (2205)
// -----------
// 5020 = 2576
// Example B
// -----------
// a = "1130"
// b = "157"
//      Decimal
// 1130 = 600
// 157  = 111
// -----------
// 1307 = 711
}
}``````

#### 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
*/
return me
}
func(this Addition) decimalValue(x byte) int {
return int(x) - int('0')
}
// Accepting two octal numbers and perform addition
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() {
// Example A
// -----------
// a = "563"
// b = "123"
//       Decimal
//  563 = (371)
// 4235 = (2205)
// -----------
// 5020 = 2576
// Example B
// -----------
// a = "1130"
// b = "157"
//      Decimal
// 1130 = 600
// 157  = 111
// -----------
// 1307 = 711
}``````

#### 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
*/
{
public: int decimalValue(char x)
{
return x - '0';
}
// Accepting two octal numbers and perform addition
{
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()
{
// Example A
// -----------
// a = "563"
// b = "123"
//       Decimal
//  563 = (371)
// 4235 = (2205)
// -----------
// 5020 = 2576
// Example B
// -----------
// a = "1130"
// b = "157"
//      Decimal
// 1130 = 600
// 157  = 111
// -----------
// 1307 = 711
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
*/
{
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)
{
// Example A
// -----------
// a = "563"
// b = "123"
//       Decimal
//  563 = (371)
// 4235 = (2205)
// -----------
// 5020 = 2576
// Example B
// -----------
// a = "1130"
// b = "157"
//      Decimal
// 1130 = 600
// 157  = 111
// -----------
// 1307 = 711
}
}``````

#### Output

`````` Given A : 563
Given B : 4235
Result  : 5020
Given A : 1130
Given B : 157
Result  : 1307``````
``````<?php
/*
Php program
*/
{
public	function decimalValue(\$x)
{
return ord(\$x) - ord('0');
}
// Accepting two octal numbers and perform addition
{
\$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()
{
// Example A
// -----------
// a = "563"
// b = "123"
//       Decimal
//  563 = (371)
// 4235 = (2205)
// -----------
// 5020 = 2576
// Example B
// -----------
// a = "1130"
// b = "157"
//      Decimal
// 1130 = 600
// 157  = 111
// -----------
// 1307 = 711
}
main();``````

#### Output

`````` Given A : 563
Given B : 4235
Result  : 5020
Given A : 1130
Given B : 157
Result  : 1307``````
``````/*
Node JS program
*/
{
decimalValue(x)
{
return x.charCodeAt(0) - '0'.charCodeAt(0);
}
// Accepting two octal numbers and perform addition
{
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()
{
// Example A
// -----------
// a = "563"
// b = "123"
//       Decimal
//  563 = (371)
// 4235 = (2205)
// -----------
// 5020 = 2576
// Example B
// -----------
// a = "1130"
// b = "157"
//      Decimal
// 1130 = 600
// 157  = 111
// -----------
// 1307 = 711
}
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
def decimalValue(self, x) :
return ord(x) - ord('0')

#  Accepting two octal numbers and perform addition
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() :
#  Example A
#  -----------
#  a = "563"
#  b = "123"
#        Decimal
#   563 = (371)
#  4235 = (2205)
#  -----------
#  5020 = 2576
#  Example B
#  -----------
#  a = "1130"
#  b = "157"
#       Decimal
#  1130 = 600
#  157  = 111
#  -----------
#  1307 = 711

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
def decimalValue(x)
return x.ord - '0'.ord
end

#  Accepting two octal numbers and perform addition
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()
#  Example A
#  -----------
#  a = "563"
#  b = "123"
#        Decimal
#   563 = (371)
#  4235 = (2205)
#  -----------
#  5020 = 2576
#  Example B
#  -----------
#  a = "1130"
#  b = "157"
#       Decimal
#  1130 = 600
#  157  = 111
#  -----------
#  1307 = 711
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
*/
{
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 = {
// Example A
// -----------
// a = "563"
// b = "123"
//       Decimal
//  563 = (371)
// 4235 = (2205)
// -----------
// 5020 = 2576
// Example B
// -----------
// a = "1130"
// b = "157"
//      Decimal
// 1130 = 600
// 157  = 111
// -----------
// 1307 = 711
}
}``````

#### Output

`````` Given A : 563
Given B : 4235
Result  : 5020
Given A : 1130
Given B : 157
Result  : 1307``````
``````import Foundation;
/*
Swift 4 program
*/
{
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()
{
// Example A
// -----------
// a = "563"
// b = "123"
//       Decimal
//  563 = (371)
// 4235 = (2205)
// -----------
// 5020 = 2576
// Example B
// -----------
// a = "1130"
// b = "157"
//      Decimal
// 1130 = 600
// 157  = 111
// -----------
// 1307 = 711
}
main();``````

#### Output

`````` Given A :  563
Given B :  4235
Result  :  5020
Given A :  1130
Given B :  157
Result  :  1307``````
``````/*
Kotlin program
*/
{
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
{
// Example A
// -----------
// a = "563"
// b = "123"
//       Decimal
//  563 = (371)
// 4235 = (2205)
// -----------
// 5020 = 2576
// Example B
// -----------
// a = "1130"
// b = "157"
//      Decimal
// 1130 = 600
// 157  = 111
// -----------
// 1307 = 711
}``````

#### Output

`````` Given A : 563
Given B : 4235
Result  : 5020
Given A : 1130
Given B : 157
Result  : 1307``````

## Comment

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.