Palindrome number is a number which remains same when it reverses. In C language, the user is allowed to enter any positive integer and to check, whether the given number is palindrome number or not by using the while loop.
Example1
Following is the C Program to find Palindrome number by using the while loop −
#include <stdio.h> int main(){ int num, temp, rem, rev = 0; printf("enter a number:\n"); scanf("%d", &num); temp = num; while ( temp > 0){ rem = temp %10; rev = rev *10+ rem; temp = temp /10; } printf("reversed number is = %d\n", rev); if ( num == rev ) printf("\n%d is Palindrome Number.\n", num); else printf("%d is not the Palindrome Number.\n", num); return 0; }
Output
When the above program is executed, it produces the following result −
enter a number: 1234 reversed number is = 4321 1234 is not the Palindrome Number. enter a number: 1221 reversed number is = 1221 1221 is Palindrome Number.
Example2
Consider another example for the C program to find palindrome number by using the while loop for strings.
#include <stdio.h> #include <string.h> void pal(char string[]); int main(){ char string[100]; printf("enter a string: "); gets(string); pal(string); return 0; } void pal(char string[]){ int i = 0; int length = strlen(string) - 1; while (length > i){ if(string[i++] != string[length--]){ printf("\n %s is not a palindrome", string); return; } } printf("\n %s is a palindrome string", string); }
Output
When the above program is executed, it produces the following result −
enter a string: tutorial tutorial is not a palindrome enter a string: saas saas is a palindrome string