C program to display palindrome numbers in a given range
|In this C Programming example, we will implement the program to display palindrome numbers in a given range and print the output on the screen.
1. Palindrome Number
A Palindrome is a word, string, number, or any sequence that reads the same forward and backward.
A Palindromic number is a number that reads the same when the digits are read backward.
Example: 121, 131, 313 ,323 ,444 ,565, 999, 11211, etc..
Helpful topics to understand this program better are-
- Operators in C
- Functions in C
- For and While loop in C
2. C program to display palindrome numbers in a given range
Let’s discuss the execution(kind of pseudocode) for the program to display palindrome numbers in a given range.
- Initially, the program will make a call to
displayPalindrome
function. - In
void displayPalindrome(int min, int max)
function we iterate from min to max and pass each number toisPalindrome(i)
. isPalindrome(i)
method verifies if the current number is palindrome or not.- Finally, if the number is found to be a palindrome then it is printed on the console.
Let us now implement the above execution of the program to display palindrome numbers in a given range in C.
#include <stdio.h> int isPalindrome(int number) { int reverse = 0; for (int i = number; i > 0; i /= 10) reverse = (reverse * 10) + (i % 10); return number == reverse; } // prints palindrome between min and max void displayPalindrome(int min, int max) { for (int i = min; i <= max; i++) { if (isPalindrome(i)) { printf("%d ", i); } } } int main() { displayPalindrome(1, 100); return 0; }
Output 1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99
3. Conclusion
In this C Programming example, we have discussed what are palindrome numbers and how to display palindrome numbers in a given range.
Helpful Links
Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.
Also for the example C programs please refer to C Programming Examples.
All examples are hosted on Github.
Recommended Books
An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding
Do not forget to share and Subscribe.
Happy coding!! ?