A palindromic number (also known as a numeral palindrome or a numeric palindrome) is a number (such as 16461) that remains the same when its digits are reversed. In other words, it has reflectional symmetry across a vertical axis.
The term palindromic is derived from palindrome, which refers to a word (such as rotor or racecar) whose spelling is unchanged when its letters are reversed.
For example, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202 are some of the Palindrome Numbers.
Now lets see the Palindrome program in C
Table of Contents
Palindrome program 1
#include <stdio.h> int main() { int n, n1, rev = 0, rem; printf("Enter any number: "); scanf("%d", &n); n1 = n; /* logic */ while (n > 0){ rem = n % 10; rev = rev * 10 + rem; n = n / 10; } if (n1 == rev){ printf("Given number is a palindromic number"); } else{ printf("Given number is not a palindromic number"); } return 0; }
Palindrome program 2
#include <stdio.h> int main() { int n, reversedN = 0, remainder, originalN; printf("Enter an integer: "); scanf("%d", &n); originalN = n; // reversed integer is stored in reversedN while (n != 0) { remainder = n % 10; reversedN = reversedN * 10 + remainder; n /= 10; } // palindrome if orignalN and reversedN are equal if (originalN == reversedN) printf("%d is a palindrome.", originalN); else printf("%d is not a palindrome.", originalN); return 0; }
Palindrome program 3
#include <stdio.h> #include <string.h> // A function to check if a string str is palindrome void isPalindrome(char str[]) { // Start from leftmost and rightmost corners of str int l = 0; int h = strlen(str) - 1; // Keep comparing characters while they are same while (h > l) { if (str[l++] != str[h--]) { printf("%s is Not Palindrome", str); return; } } printf("%s is palindrome", str); } // Driver program to test above function int main() { isPalindrome("abba"); isPalindrome("abbccbba"); isPalindrome("geeks"); return 0; }
Also see the Prime Number Check Program in C
[…] Also See the Palindrome program in C […]