Java Program to Check Whether the Given Number Is Binary or Not2 min read

In this post, we will write a program on how to check whether given number is binary or not in java. Let start by knowing the Binary number and then see the example with an explanation.

What is a Binary Number?

binary number is a number expressed in the base-2 numeral system that is the number that contains 2 symbols to represent all the numbers. The symbols are 0 and 1.

For example: 101011, 110011110, 10001111 are binary numbers.

Let’s see an explanation to find out whether the given number is binary or not in java:

There are many ways we can find the binary number in java, In this one, we will create a separate function and pass the number that needed to be check and print the result.

First, take the user input with the Scanner class and pass that number to the numBinaryOrNot() function as an argument. Then declare a boolean variable (isBinary). After that execute while loop until the passed number is not equal to zero (copyNum != 0).

Inside while loop, take the remainder and check if the remainder is greater than 1 or not. If it is greater then change the boolean value to false and break else divide the number by 10. The loop continues till all the numbers are checked.

At last, check if the boolean value is still true then the number is binary number print the result else it is not a binary number.

Java Program to Check Whether the Given Number Is Binary or Not

The output to check for the binary number in java:

Binary Number


C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …