Kotlin program to convert decimal number to octal number

Kotlin | Decimal to Octal Conversion: Here, we are going to learn how to convert a given decimal number to its equivalent octal number in Kotlin programming language? Submitted by IncludeHelp, on April 25, 2020

Kotlin - Convert decimal number to octal number

Given a number in decimal number system format, we have to convert it into octal number system format.

Example:

Input:
num = 56

Output:
70

Program to convert decimal number to octal number in Kotlin

package com.includehelp.basic

import java.util.*


//Main function Entry Point of Program
fun main(arg: Array<String>) {

    //Input Stream
    val sc = Scanner(System.`in`)

    //Input Decimal Number
    println("Enter Decimal Number  : ")
    var decimalNumber: Int = sc.nextInt()

    var octalNumber = 0
    var i = 1

    //Convert Decimal to Octal
    while (decimalNumber > 0) {
        val r= decimalNumber % 8
        octalNumber += r * i
        decimalNumber /= 8
        i *= 10
    }

    //Print Octal Number
    println("Octal is : $octalNumber")
}

Output

Run 1:
Enter Decimal Number  :
56
Octal is : 70
-------
Run 2:
Enter Decimal Number  :
100
Octal is : 144

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.