Palindrome program in C for number with example and tutorial


Introduction :        
                    Generally, A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward and forward. A number is said to be palindrome, if it reads the same both in forward and backward direction (i.e, technically - a number and its reverse should be equal).
Palindrome is an interesting example which we will go through step by step in this tutorial.
We have also explained simple palindrome example, with clear pictures for better understanding.

6886 - reads “Six thousand Six hundred and Eight Six” in both the directions. Hence, palindrome.
267 - reads “Two hundred and sixty seven” in forward direction (Left to Right) and “Seven hundred and sixty two” in backward direction (Right to Left). Hence, it is not a palindrome.

Basically what should be done is, we have an actual number 6886, ultimate aim is to find its reverse and compare actual number with the reversed one. If that's equal then its a palindrome, else No.
So lets see how to find its reverse.We have explained palindrome by means of a diagram step by step as follows :

  • Lets consider given number(gn_num) = 6886
  • We should form the reversed number  with series of iteration with some formulas.

a4 which is the last digit of the given number becomes the first digit in the reversed number;
a3 which is the second last digit of the given number becomes the second digit in the reversed number; this process goes on until all the digits from the given number gets its position in the reversed number. Now let’s see how to do this programmatically using C

Basic formulas of operation :

  1. get the last digit - Modulo with 10 (gn_num%10) returns the remainder of the given number (gn_num) on divisible with 10.
  2. Frame reversed number - The simple logic to add a digit to any number is by multiplying it with 10 and adding the new digit. (For example, if we need to add the digit 5 to 23 to make it to 235, it is done as (23*10)+5 = 235 ).
  3. discard the last digit - Division by 10 (gn_num/10) returns the quotient of the given number (gn_num) on divisible with 10.

Iteration 1 :

Iteration 2 :

Iteration 3:
Iteration 4 :

The gn_num has become 0. By now, all the digits from the given number would have occupied its position in the reversed number. Hence we can stop iterating. The point learnt from this is, we need a loop which will be executed until gn_num > 0.

Framing all these concepts together, we get the code as below, looks very simple right...
Thus we have learnt palindrome program in C language.


#include <stdio.h>
int main() {
    int gn_num, orig, rev = 0, last_digit;
    printf("Enter the number: ");
    scanf("%d", &gn_num);
    orig = gn_num; /*copy to the variable ‘orig’ in order to
                               preserve a copy of the given number*/
    while (gn_num > 0){
        last_digit = gn_num % 10;
        rev = rev * 10 + last_digit;
        gn_num = gn_num / 10;
    if (orig == rev){
        printf("The given number is palindrome"); 
        printf("The given number is not a palindrome"); 
    return 0; 

Special case:
Any single digit number is also a palindrome. Our program will satisfy this case also. 

Thus we have learnt how to find whether a given number is palindrome or not in C language. 

No comments:
Write comments

Popular Posts