Programming Tutorials and Interview Questions

Here, we develop two C programs (string and integer versions) to check if a positive integer is palindrome or not. An integer number is palindrome when the original number is equal to its reversed form. By literal definition, *palindrome* is a word, phrase, or sequence that reads the same backwards as forwards.

For example, 121 is a palindrome integer because number 121 and its reversed form are exactly the same. But, what if an integer is negative; suppose we are given -121 to check if this is palindrome. In order to check -121 for palindrome, if we first convert it into string which will be "-121" and compare it with its reversed form "121-" then check will fail and the number will not be said palindrome.

In another solution, if we don't convert the number into string and simply reverse it into a separate integer variable and compare original number with the reversed one then both numbers will be equal and the number will be said palindrome. This is the ambiguity in both solutions and to avoid confusion we drop negative numbers for palindrome check.

We will develop two C programs in order to check if a positive integer is palindrome. First solution converts the given integer into string and checks if the string is read the same backwards and forwards. Second solution reverses the given integer in a different integer variable and checks if the original and reversed number are equal. We will analyze both solutions of palindrome check and will look at their pros and cons.

Following piece of code (Program 1) first converts the given integer into string and makes a check for palindrome. We convert the number into string with the library function `sprintf`

. You can write your own routine, if you like to do so.

/* Program 1: C program to check whether given integer is palindrome number */ /* palindrome_string_ver.c */ #include <stdio.h> #include <string.h> #define NUM_LENGTH 20 //String version int isPalindrome(int n) { int palindrome = 1, length = 0, i = 0; char number[NUM_LENGTH]; //not checking for negative integers if (n < 0) { return -1; } //single digit numbers are always palindrome if (n >= 0 && n < 10) { return 1; } //convert n into string and store it in char array number. sprintf(number, "%d", n); if(number != NULL) { //get string length i.e. number of digits length = strlen(number); for(i = 0; i < length/2; i++) { if(number[i] != number[length - 1 - i]) { palindrome = 0; break; } } } return palindrome; } int main (void) { int n; printf ("Enter an integer: "); scanf ("%d", &n); if (isPalindrome(n) == -1) { printf("%d is negative. Palindrome check is done for +ve numbers.\n", n); } else if (isPalindrome(n) == 1) { printf("%d is palindrome.\n", n); } else { printf("%d is not palindrome.\n", n); } } OUTPUT ====== [root@host ~/cprogs]$ ./palindrome_string_ver Enter an integer: 343 343 is palindrome. [root@host ~/cprogs]$ ./palindrome_string_ver Enter an integer: -121 -121 is negative. Palindrome check is done for +ve numbers. [root@host ~/cprogs]$ ./palindrome_string_ver Enter an integer: 1 1 is palindrome.

Above solution for checking whether a positive integer is palindrome or not is not very efficient for two reasons. First, it requires auxiliary memory to store the converted string. Second it takes more time than the solution we are going to present next. Above solution has to first convert the number into string then it checks if the string is palindrome.

As we know, it is easy to get digits from right to left via the / and % operators. For example, digits in the number 123 from right to left are 3, 2, and 1. A reversed number 321 is constructed with these three digits. Let's check whether the reversed number is identical to the original one. If it is, the original number is a palindrome. Following C program demonstrates that.

/* Program 2: C program to check whether given integer is palindrome number */ /* palindrome_int_ver.c */ #include <stdio.h> #include <string.h> int isPalindrome (int n) { int temp = n, revN = 0; //not checking for negative integers if (n < 0) { return -1; } //single digit numbers are always palindrome if (n >= 0 && n < 10) { return 1; } while (temp != 0) { revN = revN * 10 + temp % 10; temp = temp / 10; } if (n == revN) { return 1; } else { return 0; } } int main (void) { int n; printf ("Enter an integer: "); scanf ("%d", &n); if (isPalindrome(n) == -1) { printf("%d is negative. Palindrome check is done for +ve numbers.\n", n); } else if (isPalindrome(n) == 1) { printf("%d is palindrome.\n", n); } else { printf("%d is not palindrome.\n", n); } } OUTPUT ====== [root@host ~/cprogs]$ ./palindrome_int_ver Enter an integer: 343 343 is palindrome. [root@host ~/cprogs]$ ./palindrome_int Enter an integer: -121 -121 is negative. Palindrome check is done for +ve numbers. [root@host ~/cprogs]$ ./palindrome_int Enter an integer: 1 1 is palindrome.

Above solution is faster and consumes less memory than the former one. But, it may fail at times. Reversing an integer may cause overflow depending upon the input. Suppose a 16-bit integer can hold biggest positive value 32767, if you reverse it then it would be 76723 that will cause overflow.

Hope you have enjoyed reading C programs for palindrome number. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!

Researchers say the WannaCry ransom note was poorly translated - possibly using Google Translate.

Chief executive Alex Cruz says flight disruption at Heathrow and Gatwick had nothing to do with cost cutting.

Delivering a lecture at the Hay Festival, the actor said society could face "dire consequences".