This is a C Program to check whether the given number is palindrome or not using bitwise operator.
This C Program Checks whether the given Number is Palindrome or not using Bitwise Operator.
Take input from the user and performs bitwise operations as shown in the program below.
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; }
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.
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.
$ 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
- Watch Advanced C Programming Videos
- Check Computer Science Books
- Check C Books
- Apply for Computer Science Internship
- Apply for C Internship