C Program to reverse a number and check for Palindrome

For a better understanding of this C program example, we always recommend you to learn the basic topics of C programming listed below:

What is a Palindrome number or a String?

C program explains how to reverse a given integer and check whether it is a palindrome. A palindrome number is a number that will be the same if we read it in reverse, which means the number or a string will be the same if we read from backward or forward. For example, '121' is a palindrome number. Again in the case of string 'madam' or 'civic' is a palindrome string. So to check for a palindrome number, we have to get the number in reverse and check if both are the same or not. 

How to implement a Palindrome number check in C?

In this C program, we initialize a variable 'rev' to zero. After accepting the number as input, save the number to a temp variable, use a 'while loop.' inside that loop, until the number is more significant than zero, using a mod operator with '10' to get digit (remainder) separated from the number. Add that to reverse using rev = rev * 10 + digit. Finally, divide the number by 10 to remove the last digit of the integer. Finally, check the number, and the reversed number is the same using the if condition. If that is the same, then print the number as a palindrome. Else not a palindrome.

ALGORITHM

STEP 1: Include the Header file to access the Libraries and the built-in functions.

STEP 2: Declare and Define the variables using in the C program.

STEP 3: Accept the number using printf and scanf built-in functions and save that number in a variable.

STEP 4: Store the number in a temporary variable to keep the original number safe.

STEP 5: Open a While loop until the number greater than Zero to make the number  Reverse.

STEP 7: Use the Mod operator with 10 to get the number last Digit and save that in a variable.

STEP 8: Add the Digit into the Reversed number variable by using the equation rev * 10 + Digit; (we are multiplying the number with 10 to get the added digit position to be corrected.)

STEP 9: Divide the number with 10 to remove the last Digit and continue that loop until the number is zero.

STEP 10: Check the number and reversed number is the same using the if condition.

STEP 11: If that condition is a match, then print it's a palindrome number else print, not a palindrome.

C Source Code

                                          #include <stdio.h>

void main() {
  int num, temp, digit, rev = 0;
  printf("Enter an integer\n");
  scanf("%d", & num);
  temp = num; /* original number is stored at temp */
  while (num > 0) {
    digit = num%10; /* use mod operator to take the last digit of the number */
    rev = rev * 10 + digit; /* add the digit to rev *10 to assign the digit to position */
    num /= 10; /* divide the number by 10 to remove the last digit */
  }
  printf("Given number is = %d\n", temp);
  printf("Its reverse is  = %d\n", rev);
  if (temp == rev)
    printf("Number is a palindrome\n"); /* check both variables are equal if so it is palindrome else not */
  else
    printf("Number is not a palindrome\n");
}
                                      

OUTPUT

RUN 1

Enter an integer
12321

Given number is = 12321
Its reverse is  = 12321
Number is a palindrome

RUN 2

Enter an integer
3456

Given number is = 3456
Its reverse is  = 6543
Number is not a palindrome