
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Palindrome Number Using While Loop in C
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:
"); 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.
#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
Advertisements