This blog is under construction

Sunday 23 June 2013

C program to check whether a number is palindrome or not

Write a C program to check whether the given number is palindrome or not.

If the reverse of a number equals to itself, then that number is palindrome.
Example: Reverse of 1221 is 1221.  So, 1221 a palindrome.


  #include <stdio.h>
  int main() {
        int n, data, rem, rev = 0;

        /* get the input number from user */
        printf("Enter ur value for data:");
        scanf("%d", &data);
        n = data;

        /* find the reverse for the given input */
        while (n > 0) {
                rem = n % 10;
                rev = (rev * 10) + rem;
                n = n / 10;
        }
        printf("Given data %d and its "
                "reverse is %d\n", data, rev);

        /* if the reverse of a number equals itself - palindrome */
        if (data == rev)
                printf("The given number is a palindrome\n");
        else
                printf("Given number is not a palindrome\n");
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter ur value for data:1221
  Given data 1221 and its reverse is 1221
  The given number is a palindrome



No comments:

Post a Comment