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

Palindrome is a word, string, number, or any sequence that reads the same forward and backward.

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-


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.

  1. Initially, the program will make a call to displayPalindrome function.
  2. In void displayPalindrome(int min, int max) function we iterate from min to max and pass each number to isPalindrome(i).
  3. isPalindrome(i) method verifies if the current number is palindrome or not.
  4. 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!! ?

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index