Check if a number is binary or not in Java
Given a number N, the task is to check first whether the given number is binary or not and its value should be greater than 1. print true if N is the binary representation else print false.
Examples:
Input: N = 1010
Output: true
Explanation:
Given number is greater than 1 and none of its digits is greater than 1. Thus, it is a binary number greater than 1.Examples: N = 1234
Output: false
Explanation:
Given number is greater than 1 but some of its digits { 2, 3, 4} are greater than 1. Thus, it is not a binary number.
Iterative Approach:
- Check if the number is less than or equal to 1. If it is then, print false.
- Else if number is greater than 1 then, check if every digits of the number is 1 or 0.
- If any digits of the number is greater than 1 then print false, else print true.
Below is the implementation of the above approach:
Java
// Java program for the above approach
class GFG {
// Function to check if number
// is binary or not
public static boolean isBinaryNumber(int num)
{
// Return false if a number
// is either 0 or 1 or a
// negative number
if (num == 0 || num == 1
|| num < 0) {
return false;
}
// Get the rightmost digit of
// the number with the help
// of remainder '%' operator
// by dividing it with 10
while (num != 0) {
// If the digit is greater
// than 1 return false
if (num % 10 > 1) {
return false;
}
num = num / 10;
}
return true;
}
public static void main(String args[])
{
// Given Number N
int N = 1010;
// Function Call
System.out.println(isBinaryNumber(N));
}
}
Please Login to comment...