Program to check whether number is binary or not in Kotlin
/**
* Kotlin Program to check given number is binary number or not
*/
package com.includehelp.basic
import java.util.*
// Function to check Binary Number
fun isBinaryNumber(binaryNumber: Long): Boolean {
var binaryNumber = binaryNumber
while (binaryNumber > 0) {
if (binaryNumber % 10 > 1) {
return false
}
binaryNumber /= 10
}
return true
}
//Main Function, Entry Point of program
fun main(args: Array<String>) {
val sc = Scanner(System.`in`)
//Input Binary Number
println("Enter Number : ")
val binaryNumber: Long = sc.nextLong()
if (isBinaryNumber(binaryNumber))
println("Binary Number")
else
println("Number is not Binary")
}
Output
RUN 1:
Enter Number :
11001101010101010
Binary Number
---
RUN 2:
Enter Number :
101010102
Number is not Binary
Program to check whether number is binary or not in Kotlin
Output
need an explanation for this answer? contact us directly to get an explanation for this answer