C program to find palindrome number by using while loop


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 −

 Live Demo

#include <stdio.h>
int main(){
   int num, temp, rem, rev = 0;
   printf("enter a number:
");    scanf("%d", &num);    temp = num;    while ( temp > 0){       rem = temp %10;       rev = rev *10+ rem;       temp = temp /10;    }    printf("reversed number is = %d
", rev);    if ( num == rev )       printf("
%d is Palindrome Number.
", num);    else       printf("%d is not the Palindrome Number.
", 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.

 Live Demo

#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("
%s is not a palindrome", string);          return;       }    }    printf("
%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

Updated on: 24-Mar-2021

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements