C Program to Reverse a Number and Check if it is a Palindrome

This is a C Program which reverses a number & checks if it is a palindrome or not.

Problem Description

This C program accepts an integer, reverse it and also checks if it is a palindrome or not.

Problem Solution

1. Take the number which you have to reverse as the input.
2. Obtain its quotient and remainder.
3. Multiply the separate variable with 10 and add the obtained remainder to it.
4. Do step 2 again for the quotient and step 3 for the remainder obtained in step 4.
5. Repeat the process until quotient becomes zero.
6. When it becomes zero, check if the reversed number is equal to original number or not.
7. Print the output and exit.

Program/Source Code

Here is source code of the C program to reverse a number & checks it is a palindrome or not. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1.  
  2.  
  3. #include <stdio.h>
  4.  
  5. void main()
  6. {
  7.     int num, temp, remainder, reverse = 0;
  8.  
  9.     printf("Enter an integer \n");
  10.     scanf("%d", &num);
  11.     /*  original number is stored at temp */
  12.     temp = num;
  13.     while (num > 0)
  14.     {
  15.         remainder = num % 10;
  16.         reverse = reverse * 10 + remainder;
  17.         num /= 10;
  18.     }
  19.     printf("Given number is = %d\n", temp);
  20.     printf("Its reverse is  = %d\n", reverse);
  21.     if (temp == reverse)
  22.         printf("Number is a palindrome \n");
  23.     else
  24.         printf("Number is not a palindrome \n");
  25. }
Program Explanation

1. Take the number which you have to reverse as the input and store it in the variable num.
2. Copy the input number to the another variable temp.
3. Firstly initialize the variable reverse to zero.
4. Obtain the remainder of the input number.
5. Multiply the variable reverse with 10 and add the Obtained remainder to it and store the result in the same variable.
6. Obtain the quotient of the input number and considering this as input number repeat the steps as mentioned above until the obtained quotient becomes zero.
7. When it becomes zero, using if,else statement check whether the reversed number is equal to original number or not.
8. If it is equal, then print the output as “Number is a palindrome”, otherwise print the output as “Number is not a palindrome”.

advertisement
advertisement
Runtime Test Cases
Case:1
Enter an integer
6789
Given number is = 6789
Its reverse is  = 9876
Number is not a palindrome
 
Case:2
Enter an integer
58085
Given number is = 58085
Its reverse is  = 58085
Number is a palindrome

Sanfoundry Global Education & Learning Series – 1000 C Programs.

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

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples.

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.