Program to print all palindromes in a given range in C++

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to print all palindromes in a given range.

For this we will be given the mathematical range in which the palindromes are to be found. Our task is to find all the palindromes in that range and print it back.

Example

 Live Demo

#include<iostream>
using namespace std;
//checking if the number is a palindrome
int is_palin(int n){
   int rev = 0;
   for (int i = n; i > 0; i /= 10)
   rev = rev*10 + i%10;
   return (n==rev);
}
void countPal(int min, int max){
   for (int i = min; i <= max; i++)
   if (is_palin(i))
   cout << i << " ";
}
int main(){
   countPal(99, 250);
   return 0;
}

Output

99 101 111 121 131 141 151 161 171 181 191 202 212 222 232 242
raja
Published on 19-Dec-2019 15:04:21
Advertisements