kotlin 16进制
Given a number in octal number system format, we have to convert it into decimal number system format.
给定八进制系统格式的数字,我们必须将其转换为十进制系统格式。
Example:
例:
Input:
num = 344
Output:
228
在Kotlin中将八进制数转换为十进制数的程序 (Program to convert octal number to decimal 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 Octal Number
println("Enter Octal Number : ")
var octalNumber: Int = sc.nextInt()
var decimalNumber=0
var i = 0
var isCorrectOctal=true;
val octalStr = octalNumber.toString();
//Check Given no is valid Ocatal Number or not
for(i in octalStr.indices){
if (octalStr[i] !in '0'..'7' ){
isCorrectOctal=false
}
}
if(isCorrectOctal){
//Convert Octal to Decimal
while (octalNumber > 0) {
val r= octalNumber % 10
decimalNumber += (r * Math.pow(8.0,i.toDouble())).toInt()
octalNumber /= 10
i ++
}
//Print Decimal Number
println("Decimal: $decimalNumber")
}else{
System.err.println("Invalid Octal Number !!")
}
}
Output
输出量
Run 1:
Enter Octal Number :
344
Decimal: 228
-------
Run 2:
Enter Octal Number :
48
Invalid Octal Number !!
-------
Run 3:
Enter Octal Number :
47
Decimal: 39
翻译自: https://www.includehelp.com/kotlin/convert-octal-number-to-decimal-number.aspx
kotlin 16进制