kotlin 判断数字
Given an integer number, we have to find reverse number and print it.
给定一个整数,我们必须找到反向数字并打印出来。
Example:
例:
Input:
Number: 12345
Output:
Reverse Number: 54321
To find a reverse number – we use this formula: reverse = (reverse * 10) + lastDigit, to calculate the lastDigit, we find the remainder of the number by dividing the number with 10.
要找到一个反向数字 –我们使用以下公式: reverse =(reverse * 10)+ lastDigit ,要计算lastDigit ,我们可以通过将数字除以10来找到数字的余数。
Consider the below program to find the reverse number.
考虑下面的程序以找到反向编号 。
计划在Kotlin中反转数字 (Program to reverse a number in Kotlin)
package com.includehelp.basic
import java.util.*
/* Function to Reverse an given Integer Number */
fun getReverse(number: Int): Int {
var number = number
var sum = 0
while (number > 0) {
val r = number % 10
sum = sum * 10 + r
number /= 10
}
return sum
}
//Main function Entry Point of Program
fun main(arg: Array<String>) {
val sc = Scanner(System.`in`)
//Input Number
println("Enter Number : ")
val num: Int = sc.nextInt()
//Calling function to get reverse number
val reverseNumber = getReverse(num)
// Print reverse format of number
println("Reverse Number : $reverseNumber")
}
Output
输出量
Run 1:
Enter Number :
12345
Reverse Number : 54321
-------
Run 2:
Enter Number :
345389
Reverse Number : 983543
翻译自: https://www.includehelp.com/kotlin/reverse-a-number.aspx
kotlin 判断数字