Q:

Kotlin program to convert decimal number to octal number

belongs to collection: Kotlin Basic Programs

0

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

Example:

    Input:
    num = 56

    Output:
    70

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Kotlin Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Kotlin program to convert binary number to octal n... >>
<< Kotlin program to convert decimal to binary...