Kotlin program to check whether number is binary or not

Kotlin | Check number is binary or not: Here, we are going to learn how to check whether the given number is binary or not in Kotlin programming language? Submitted by IncludeHelp, on April 22, 2020

Kotlin - Check whether number is binary or not

Given a number, we have to check whether given number is a binary or not.

Example:

Input:
start = 15
end = 700

Output:
[153, 370, 371, 407]

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

Comments and Discussions!

Load comments ↻





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