# Swift Program to convert Decimal to Octal

This tutorial will discuss how to write Swift program to convert Decimal to Octal.

Decimal numbers are those numbers whose base value is 10. Decimal numbers are also known as base 10 number system which contain 10 numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. Here, the position of every digit in the decimal number has weight is a power of 10.

Octal numbers are those numbers whose base value is 8. Octal numbers are also known as base 8 number system which contain 8 numbers: 0, 1, 2, 3, 4, 5, 6, 7. Here, the position of every digit in the octal number has weight is a power of 8.

Now we convert the decimal number(base-10) into octal number system(base-8) using any of the following method.

Below is a demonstration of the same −

Input

Suppose our given input is −

Decimal number = 10


Output

The desired output would be −

Octal number = 12


## Method1: CONVERT A DECIMAL TO OCTAL USING DIVISION METHOD

To convert a decimal to octal we divide the number by 8 and store the remainder in a variable till the number is equal to 0.

### Algorithm

Following is the algorithm −

• Step 1 − Initialise a variable with decimal number.

• Step 2 − Initialise octalNumber = 0 and count = 1

• Step 3 − Find the remainder when decimal number is divide by 8.

• Step 4 − Update the octal number value

octalNumber += rem * count

• Step 5 − Now increment the value of count

count = count * 10

• Step 6 − Divide the given decimal number by 8

number /= 8

• Step 7 − Repeat above steps till the value of number become 0.

• Step 8 − Print the output.

### Example

The following program shows how to convert Decimal to Octal.

import Foundation
import Glibc

// Decimal number
var number = 29

var octalNumber = 0
var count = 1

print("Decimal Number:", number)

// Checking if the given number is not zero
while(number != 0) {

// Calculating remainder
let rem = number % 8

// Storing octal number
octalNumber += rem * count

// Storing exponential value
count = count * 10

// Dividing the number by 8
number /= 8

}

print("Resultant Octal Number:", octalNumber)


### Output

Decimal Number: 29
Resultant Octal Number: 35


Here in the above code, we convert the decimal number into octal number using the below code −

while(number != 0) {
let rem = number % 8
octalNumber += rem * count
count = count * 10
number /= 8
}


So the working of the above code is −

Initially, Number = 29
octalNumber = 0
count = 1
1st iteration:
while(29 != 0) // condition is true {
let rem = number % 8 = 29 % 8 = 5
octalNumber += rem * count = 0 + 5 * 1 = 5
count = count * 10 = 1 * 10 = 10
number /= 8 = 29/8 = 3
}
2st iteration:
while(3 != 0) // condition is true {
let rem = number % 8 = 3 % 8 = 3
octalNumber += rem * count = 5 + 3 * 10 = 35
count = count * 10 = 10 * 10 = 100
number /= 8 = 3/8 = 0
}
3rd iteration:
while(0 != 0) // condition is false {
}
// Exit from the loop and print 35.


## Method 2 CONVERT DECIMAL TO OCTAL NUMBER SYSTEM USING STRING(_:RADIX:).

We can also convert decimal to octal number system using String(_:radix:). This method creates a new value from the given string and radix.

### Syntax

Following is the syntax −

String(value, radix: base)


Here, value is the ASCII representation of a number. Whereas radix is used to convert text into integer value. The default value of radix is 10 and it can be in the range from 2…36.

### Example

The following program shows how to convert Decimal to Octal.

import Foundation
import Glibc

// Decimal number
var number = 18

print("Decimal Number:", number)

// Finding octal number
if let octalNumber = Int(String(number, radix: 8)){
print("Octal Number:", octalNumber)
}


### Output

Decimal Number: 18
Octal Number: 22


Here, we convert the decimal number 18 into octal number using the following code −

if let octalNumber = Int(String(number, radix: 8)){
print("Octal Number:", octalNumber)
}


Where String(number, radix: 8) convert the given decimal number into octal number string further using Int() we convert the string into integer value. Hence the resultant octal number is 22.