Check if binary representation of a number is palindrome in Java
In this tutorial, I will be explaining if the binary of a number is palindrome or not in Java. A palindrome number is one which when reversed, yields the same number with which we started.
As stated above, to check if a number is palindrome or not, we need to find the reverse of it. I will be using the basic OOPS concepts of Java to implement my approach to this problem. In my class, I have made three functions: decimaltobinary(), check_pallindrome(), and the main() function.
The decimaltobinary() function takes the integer number as parameterized input and converts it into its binary equivalent. I have stored the result in a 1-D array. Since I have to return this array in the main function, the return type of this function should of integer array type.
In the next function, check_pallindrome(), I have reversed the array and stored in another 1_D array. After that, I will check that each value in these two arrays at the same index is the same. A flag variable helps me keep a check on this and whenever the condition fails, I break out of the loop. If at the end the flag variable is still set 1, then it is a palindrome number otherwise it is not. One important thing to note here is that I have made this function static. I have done this because we will have to pass an array as a parameter to the function. It cannot be done if our function is not static and will generate a compile-time error.
Also read: Java Program To Check A Number is Palindrome or Not
In the main() function, I have made an object of Scanner class to take user input. Next, I have created an object of the class to call my functions in main. I store the results of decimaltobinary() function in an array and pass that as a parameter in check_pallindrome() function.
I have shown my code below:
import java.util.*; public class binary_pallindrome { int[] decimaltobinary(int a) { int i, k=7; int b[] = new int[8]; //changing to binary representation while(a>0) { if(a%2==0) { b[k] = 0; k--; a = a/2; } else { b[k] = 1; k--; a = a/2; } } return b; } static void check_pallindrome(int A[]) { int m = 7; int i, flag=0; int B[] = new int[8]; //Reversing the original and storing in another array for(i=0;i<8;i++) { B[m] = A[i]; m--; } System.out.print("Binary "); for(i=0;i<8;i++) { System.out.print(A[i]); } System.out.println(" "); System.out.print("Reversed Binary "); for(i=0;i<8;i++) { System.out.print(B[i]); } System.out.println(" "); //setting the flag appropriately for(i=0;i<8;i++) { if(A[i] == B[i]) { flag = 1; } else { flag=0; break; } } //check flag variable if(flag == 1) { System.out.println("Pallindrome"); } else { System.out.println("Not Pallindrome"); } System.out.println(" "); } public static void main(String[] args) { Scanner ob = new Scanner(System.in); System.out.println("Enter number "); int n = ob.nextInt(); binary_pallindrome bp = new binary_pallindrome(); int arr[] = bp.decimaltobinary(n); check_pallindrome(arr); } }
Input:
In the main function, I just have to input our initial decimal number.
Output:
Enter number 153 Binary 10011001 Reversed Binary 10011001 Pallindrome
Leave a Reply