C Program to Check Whether a Number is Palindrome or not using Bitwise Operator

This is a C Program to check whether the given number is palindrome or not using bitwise operator.

Problem Description

This C Program Checks whether the given Number is Palindrome or not using Bitwise Operator.

Problem Solution

Take input from the user and performs bitwise operations as shown in the program below.

Program/Source Code

Here is source code of the C Program to Check whether the given Number is Palindrome or not using Bitwise Operator. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to Check whether the given Number is Palindrome 
 * or not using Bitwise Operator
 */
#include <stdio.h>
#include <string.h>
#define SIZE 8
/* Function Prototype */
int is_palindrome(unsigned char[]);
 
void main()
{
    int num, num1 = 0, i = 0, j = SIZE - 1, res;
    unsigned char c[SIZE];
    printf("Enter a number(max 255)");
    scanf("%d", &num);
    num1 = num;
    while (num != 0)
    {
        c[j] = num&1;
        j--;
        num = num>>1; /* Shifting right the given number by 1 bit */
    }
    printf("The number %d in binary is:", num1);
    for (i = 0;i < SIZE;i++)
    {
        printf("%d", c[i]);
    }
    res = is_palindrome(c);    /* Calling Function */
    if (res == 0)
    {
        printf("\nNUMBER IS PALINDROME\n");
    }
    else
    {
        printf("\nNUMBER IS NOT PALINDROME\n");
    }
}
 
/* Code to check if the number is palindrome or not */
int is_palindrome(unsigned char c[])
{
    char temp[SIZE];
    int i, j, flag = 0;
    for (i = 0, j = SIZE - 1;i < SIZE, j >= 0;i++, j--)
    {
        temp[j] = c[i];
    }
    for (i = 0;i < SIZE;i++)
    {
        if (temp[i] != c[i])
        {
            flag = 1;
        }
    }
    return flag;
}
Program Explanation

In this C program, we are reading the value of ‘string using word character array variable. A palindrome is a word, phrase or sentence that reads the same backward or forward.

advertisement
advertisement

Check the values of a given string and reverse string are equal. If else condition statement is used inside for loop if the condition is true then assign the value of ‘flag’ variable as 1. Otherwise, if the condition is false then execute the else statement. Assign the value of ‘flag’ variable as 0 and exit the loop.

If else condition statement is used to check the value of ‘flag’ variable is equal to1. If the condition is true then execute the statement and print the string is palindrome. Otherwise, if the condition is false then execute the else statement and print as not a palindrome.

Runtime Test Cases
 
$ cc bits21.c
$ a.out
Enter a number(max 255)153
The number 153 in binary is:10011001
NUMBER IS PALINDROME
 
$ a.out
Enter a number(max 255)24
The number 24 in binary is:00011000
NUMBER IS PALINDROME

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.