Posted on by Kalkicode
Code Conversion

# Convert decimal to octal number

Decimal and octal are both number systems used to represent numbers. The decimal number system uses ten digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) and the base or radix is 10. On the other hand, the octal number system uses eight digits (0, 1, 2, 3, 4, 5, 6, 7) and the base or radix is 8.

Converting a decimal number to an octal number involves dividing the decimal number by 8 and recording the remainder at each step until the quotient becomes 0. The octal number is then obtained by writing the remainders in reverse order.

For example, to convert the decimal number 23 to octal:

1. Divide 23 by 8. The quotient is 2 and the remainder is 7. Write down the remainder (7).
2. Divide 2 by 8. The quotient is 0 and the remainder is 2. Write down the remainder (2).
3. The octal number is obtained by writing the remainders in reverse order: 23(decimal) = 27(octal).

Therefore, the octal representation of the decimal number 23 is 27.

## Program Solution

``````//C Program
//Convert decimal to octal number
#include <stdio.h>

void decimal_to_octal(int number)
{
int result = 0, multiplier = 1;
int remainder = 0;
printf("Decimal : %d  ", number);
int flag = 0;
if (number < 0)
{
//When number is negative number
number = -number;
flag = 1;
}
//Convert given number into an octal
while (number != 0)
{
remainder = number % 8;
result = (remainder * multiplier) + result;
multiplier *= 10;
number /= 8;
}
if (flag == 1)
{
result = -result;
}
printf(" Octal : %d \n\n", result);
}
int main()
{
//Test Case
decimal_to_octal(18);
decimal_to_octal(10);
decimal_to_octal(-123);
decimal_to_octal(49);
decimal_to_octal(100);
decimal_to_octal(16);
return 0;
}``````

#### Output

``````Decimal : 18   Octal : 22

Decimal : 10   Octal : 12

Decimal : -123   Octal : -173

Decimal : 49   Octal : 61

Decimal : 100   Octal : 144

Decimal : 16   Octal : 20
``````
``````/*
Java Program
Convert decimal to octal number
*/
public class MyNumber
{
public void decimal_to_octal(int number)
{
int result = 0, multiplier = 1;
int remainder = 0;
System.out.print("Decimal : " + number + " ");
int flag = 0;
if (number < 0)
{
//When number is negative number
number = -number;
flag = 1;
}
//Convert given number into an octal
while (number != 0)
{
remainder = number % 8;
result = (remainder * multiplier) + result;
multiplier *= 10;
number /= 8;
}
if (flag == 1)
{
result = -result;
}
System.out.print(" Octal : " + result + " \n\n");
}
public static void main(String[] args)
{
MyNumber obj = new MyNumber();
//Test Cases
obj.decimal_to_octal(18);
obj.decimal_to_octal(10);
obj.decimal_to_octal(-123);
obj.decimal_to_octal(49);
obj.decimal_to_octal(100);
obj.decimal_to_octal(16);
}
}``````

#### Output

``````Decimal : 18  Octal : 22

Decimal : 10  Octal : 12

Decimal : -123  Octal : -173

Decimal : 49  Octal : 61

Decimal : 100  Octal : 144

Decimal : 16  Octal : 20
``````
``````//Include header file
#include <iostream>

using namespace std;
/*
C++ Program
Convert decimal to octal number
*/
class MyNumber
{
public: void decimal_to_octal(int number)
{
int result = 0, multiplier = 1;
int remainder = 0;
cout << "Decimal : " << number;
int flag = 0;
if (number < 0)
{
//When number is negative number
number = -number;
flag = 1;
}
//Convert given number into an octal
while (number != 0)
{
remainder = number % 8;
result = (remainder * multiplier) + result;
multiplier *= 10;
number /= 8;
}
if (flag == 1)
{
result = -result;
}
cout << " Octal : " << result << " \n\n";
}
};
int main()
{
MyNumber obj = MyNumber();
//Test Cases
obj.decimal_to_octal(18);
obj.decimal_to_octal(10);
obj.decimal_to_octal(-123);
obj.decimal_to_octal(49);
obj.decimal_to_octal(100);
obj.decimal_to_octal(16);
return 0;
}``````

#### Output

``````Decimal : 18 Octal : 22

Decimal : 10 Octal : 12

Decimal : -123 Octal : -173

Decimal : 49 Octal : 61

Decimal : 100 Octal : 144

Decimal : 16 Octal : 20
``````
``````//Include namespace system
using System;
/*
C# Program
Convert decimal to octal number
*/
public class MyNumber
{
public void decimal_to_octal(int number)
{
int result = 0, multiplier = 1;
int remainder = 0;
Console.Write("Decimal : " + number);
int flag = 0;
if (number < 0)
{
//When number is negative number
number = -number;
flag = 1;
}
//Convert given number into an octal
while (number != 0)
{
remainder = number % 8;
result = (remainder * multiplier) + result;
multiplier *= 10;
number /= 8;
}
if (flag == 1)
{
result = -result;
}
Console.Write(" Octal : " + result + " \n\n");
}
public static void Main(String[] args)
{
MyNumber obj = new MyNumber();
//Test Cases
obj.decimal_to_octal(18);
obj.decimal_to_octal(10);
obj.decimal_to_octal(-123);
obj.decimal_to_octal(49);
obj.decimal_to_octal(100);
obj.decimal_to_octal(16);
}
}``````

#### Output

``````Decimal : 18 Octal : 22

Decimal : 10 Octal : 12

Decimal : -123 Octal : -173

Decimal : 49 Octal : 61

Decimal : 100 Octal : 144

Decimal : 16 Octal : 20
``````
``````<?php
/*
Php Program
Convert decimal to octal number
*/
class MyNumber
{
public	function decimal_to_octal(\$number)
{
\$result = 0;
\$multiplier = 1;
\$remainder = 0;
echo "Decimal : ". \$number;
\$flag = 0;
if (\$number < 0)
{
//When number is negative number
\$number = -\$number;
\$flag = 1;
}
//Convert given number into an octal
while (\$number != 0)
{
\$remainder = \$number % 8;
\$result = (\$remainder * \$multiplier) + \$result;
\$multiplier *= 10;
\$number = intval(\$number / 8);
}
if (\$flag == 1)
{
\$result = -\$result;
}
echo " Octal : ". \$result ." \n\n";
}
}

function main()
{
\$obj = new MyNumber();
//Test Cases
\$obj->decimal_to_octal(18);
\$obj->decimal_to_octal(10);
\$obj->decimal_to_octal(-123);
\$obj->decimal_to_octal(49);
\$obj->decimal_to_octal(100);
\$obj->decimal_to_octal(16);
}
main();``````

#### Output

``````Decimal : 18 Octal : 22

Decimal : 10 Octal : 12

Decimal : -123 Octal : -173

Decimal : 49 Octal : 61

Decimal : 100 Octal : 144

Decimal : 16 Octal : 20
``````
``````/*
Node Js Program
Convert decimal to octal number
*/
class MyNumber
{
decimal_to_octal(number)
{
var result = 0;
var multiplier = 1;
var remainder = 0;
process.stdout.write("Decimal : " + number);
var flag = 0;
if (number < 0)
{
//When number is negative number
number = -number;
flag = 1;
}
//Convert given number into an octal
while (number != 0)
{
remainder = number % 8;
result = (remainder * multiplier) + result;
multiplier *= 10;
number = parseInt(number / 8);
}
if (flag == 1)
{
result = -result;
}
process.stdout.write(" Octal : " + result + " \n\n");
}
}

function main()
{
var obj = new MyNumber();
//Test Cases
obj.decimal_to_octal(18);
obj.decimal_to_octal(10);
obj.decimal_to_octal(-123);
obj.decimal_to_octal(49);
obj.decimal_to_octal(100);
obj.decimal_to_octal(16);
}
main();``````

#### Output

``````Decimal : 18 Octal : 22

Decimal : 10 Octal : 12

Decimal : -123 Octal : -173

Decimal : 49 Octal : 61

Decimal : 100 Octal : 144

Decimal : 16 Octal : 20
``````
``````#   Python 3 Program
#   Convert decimal to octal number

class MyNumber :
def decimal_to_octal(self, number) :
result = 0
multiplier = 1
remainder = 0
print("Decimal : ", number, end = "")
flag = 0
if (number < 0) :
# When number is negative number
number = -number
flag = 1

# Convert given number into an octal
while (number != 0) :
remainder = number % 8
result = (remainder * multiplier) + result
multiplier *= 10
number = int(number / 8)

if (flag == 1) :
result = -result

print(" Octal : ", result ," \n\n", end = "")

def main() :
obj = MyNumber()
# Test Cases
obj.decimal_to_octal(18)
obj.decimal_to_octal(10)
obj.decimal_to_octal(-123)
obj.decimal_to_octal(49)
obj.decimal_to_octal(100)
obj.decimal_to_octal(16)

if __name__ == "__main__": main()``````

#### Output

``````Decimal :  18 Octal :  22

Decimal :  10 Octal :  12

Decimal :  -123 Octal :  -173

Decimal :  49 Octal :  61

Decimal :  100 Octal :  144

Decimal :  16 Octal :  20
``````
``````#
#   Ruby Program
#   Convert decimal to octal number

class MyNumber

def decimal_to_octal(number)

result = 0
multiplier = 1
remainder = 0
print("Decimal : ", number)
flag = 0
if (number < 0)

# When number is negative number
number = -number
flag = 1
end
# Convert given number into an octal
while (number != 0)

remainder = number % 8
result = (remainder * multiplier) + result
multiplier *= 10
number = number / 8
end
if (flag == 1)

result = -result
end
print(" Octal : ", result ," \n\n")
end
end
def main()

obj = MyNumber.new()
# Test Cases
obj.decimal_to_octal(18)
obj.decimal_to_octal(10)
obj.decimal_to_octal(-123)
obj.decimal_to_octal(49)
obj.decimal_to_octal(100)
obj.decimal_to_octal(16)
end
main()``````

#### Output

``````Decimal : 18 Octal : 22

Decimal : 10 Octal : 12

Decimal : -123 Octal : -173

Decimal : 49 Octal : 61

Decimal : 100 Octal : 144

Decimal : 16 Octal : 20

``````
``````/*
Scala Program
Convert decimal to octal number
*/
class MyNumber
{
def decimal_to_octal(num: Int): Unit = {
var result: Int = 0;
var multiplier: Int = 1;
var remainder: Int = 0;
var number = num;
print("Decimal : " + number);
var flag: Int = 0;
if (number < 0)
{
//When number is negative number
number = -number;
flag = 1;
}
//Convert given number into an octal
while (number != 0)
{
remainder = number % 8;
result = (remainder * multiplier) + result;
multiplier *= 10;
number = (number / 8).toInt;
}
if (flag == 1)
{
result = -result;
}
print(" Octal : " + result + " \n\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyNumber = new MyNumber();
//Test Cases
obj.decimal_to_octal(18);
obj.decimal_to_octal(10);
obj.decimal_to_octal(-123);
obj.decimal_to_octal(49);
obj.decimal_to_octal(100);
obj.decimal_to_octal(16);
}
}``````

#### Output

``````Decimal : 18 Octal : 22

Decimal : 10 Octal : 12

Decimal : -123 Octal : -173

Decimal : 49 Octal : 61

Decimal : 100 Octal : 144

Decimal : 16 Octal : 20
``````
``````/*
Swift Program
Convert decimal to octal number
*/
class MyNumber
{
func decimal_to_octal(_ num: Int)
{
var result: Int = 0;
var multiplier: Int = 1;
var remainder: Int = 0;
var number : Int = num;
print("Decimal : ", number, terminator: "");
var flag: Int = 0;
if (number < 0)
{
//When number is negative number
number = -number;
flag = 1;
}
//Convert given number into an octal
while (number != 0)
{
remainder = number % 8;
result = (remainder * multiplier) + result;
multiplier *= 10;
number = number / 8;
}
if (flag == 1)
{
result = -result;
}
print(" Octal : ", result ," \n\n", terminator: "");
}
}
func main()
{
let obj: MyNumber = MyNumber();
//Test Cases
obj.decimal_to_octal(18);
obj.decimal_to_octal(10);
obj.decimal_to_octal(-123);
obj.decimal_to_octal(49);
obj.decimal_to_octal(100);
obj.decimal_to_octal(16);
}
main();``````

#### Output

``````Decimal :  18 Octal :  22

Decimal :  10 Octal :  12

Decimal :  -123 Octal :  -173

Decimal :  49 Octal :  61

Decimal :  100 Octal :  144

Decimal :  16 Octal :  20
``````

## 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.

Categories
Relative Post