
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
Print All Palindromes in a Given Range in C++
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
#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
Advertisements