Program to check a number for palindrome in C

Logic:

  • Palindrome: A number is palindrome if it is equal to its reverse.
  • Enter the number n
  • Example 1: n=345
  • Reverse=543
  • Number is not equal to its reverse.
  • So it is not palindrome
  • Example 2: n=121
  • Reverse=121
  • Number is equal to its reverse.
  • So it is palindrome

Algorithm

  1. Enter the number as n
  2. Copy the number n into a temporary variable, say, no
  3. First find the reverse of the number
  4. Initially assign rev=0
  5. while(n>0)
  6. {
  7. r=n%10;
  8. rev = rev * 10 + r;
  9. n=n/10;
  10. }
  11. if (rev==no)
  12. printf(“It is palindrome”);
  13. else
  14. printf(“It is not palindrome”);

Program

#include<stdio.h>
int main()
{
    printf("****************************************************************");
    printf("\n****************************************************************");
    printf("\n**         WAP to check a number for palindrome               **");
    printf("\n**               Created by Sheetal Garg                      **");
    printf("\n**                 Assistant Professor                        **");
    printf("\n**                 Phone No:9467863365                        **");
    printf("\n****************************************************************");
    printf("\n****************************************************************\n");
    int i,n,r,rev=0,no;
    printf("Enter the no n \n");
    scanf("%d",&n);
    no=n
    while(n>0)
    {
       r=n%10;
       n=n/10;
       rev=rev*10+r;
    }
    if(no==rev)
    printf("Number is palindrome");
    else
    printf("Number is not palindrome");
}

Output 1

***************************************************************************
***************************************************************************
**                    WAP to check a number for palindrome               **
**                          Created by Sheetal Garg                      **
**                            Assistant Professor                        **
**                            Phone No:9467863365                        **
***************************************************************************
***************************************************************************
Enter the no n
345
Number is not palindrome

Output 2

***************************************************************************
***************************************************************************
**                    WAP to check a number for palindrome               **
**                          Created by Sheetal Garg                      **
**                            Assistant Professor                        **
**                            Phone No:9467863365                        **
***************************************************************************
***************************************************************************
Enter the no n
121
Number is palindrome
error: You can only copy the programs code and output from this website. You are not allowed to copy anything else.