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
All palindrome numbers in a list?
Here we will see one simple problem. We have to find all numbers that are palindrome in nature in a given list. The approach is simple, take each number from list and check it is palindrome or not, and print the number.
Algorithm
getAllPalindrome(arr, n)
Begin for each element e in arr, do if e is palindrome, then print e end if done End
Example
#include <iostream>
#include <cmath>
using namespace std;
bool isPalindrome(int n){
int reverse = 0, t;
t = n;
while (t != 0){
reverse = reverse * 10;
reverse = reverse + t%10;
t = t/10;
}
return (n == reverse);
}
int getAllPalindrome(int arr[], int n) {
for(int i = 0; i<n; i++){
if(isPalindrome(arr[i])){
cout << arr[i] << " ";
}
}
}
int main() {
int arr[] = {25, 145, 85, 121, 632, 111, 858, 45};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "All palindromes: ";
getAllPalindrome(arr, n);
}
Output
All palindromes: 121 111 858
Advertisements