Q:

Kotlin program to check whether number is binary or not

belongs to collection: Kotlin Basic Programs

0

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]

All Answers

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

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

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 decimal to binary... >>
<< Kotlin program to find prime numbers in a given ra...