ADVERTISEMENT
ADVERTISEMENT

Home » Kotlin » Kotlin programs » Kotlin basic programs

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

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
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.