Check whether the Binary number is Palindrome or not

Check whether the Binary number is Palindrome or not

Question:

Convert the given decimal number into binary then check whether it is Palindrome or not without using array.

 Logic:

  1. Get the decimal number from user.
  2. Convert the decimal number into binary.
  3. Check the first and last digit of binary number which is equal or not.
  4. Then remove the first and last digit of binary number.
  5. Repeat the step 3 and 4 until binary value becomes zero.

Program:

#include<stdio.h>
void main()
{
    int n,i=0,j=1,k=0,m=1;
    printf("Enter the number");
    scanf("%d",&n);
    while(n>0)                  //converts decimal to binary
    {
        k=k+(n%2)*m;
        m*=10;
        n/=2;
    }n=k;
    m/=10;
    while(k>0)
    {
        i=k/m;                 //first digit of binary number
        j=k%10;                //last digit of binary number
        if(i!=j)               //check both first and last digit
            break;
        k%=m;                  //removes the first digit
        k/=10;                 //removes the last digit
        m/=100;
    }
    if(k==0)
    {
        printf("%d is Palindrome", n);
    }
    else
    {
        printf("%d is not palindrome",n);
    }
}

You might also like….
To check whether the number is palindrome or not

Vignesh

A Computer Science graduate who likes to make things simpler. When he’s not working, you can find him surfing the web, learning facts, tricks and life hacks. He also enjoys movies in his leisure time.