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 number then check whether it is Palindrome or not without using an array.

 Logic:

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

Binary Number

A Binary is made up of only 0s and 1s. There is no 2, 3, 4, 5, 6, 7, 8, or 9 in Binary!.

          Binary number system, in mathematics, a positional numeral system employing 2 as the base and so requiring only two different symbols for its digits, 0 and 1, instead of the usual 10 different symbols needed in the decimal system. The numbers from 0 to 10 are thus in binary 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, and 1010. The importance of the binary system to information theory and computer technology derives mainly from the compact and reliable manner in which 0s and 1s can be represented in electromechanical devices with two states—such as ‘open-close’, ‘on-off’,etc.

About Binary More>>

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.

Leave a Reply

Your email address will not be published.

two × 2 =