Home >Backend Development >C++ >What are all the palindromic numbers in the list?

What are all the palindromic numbers in the list?

王林
王林forward
2023-09-10 11:25:021234browse

What are all the palindromic numbers in the list?

Here we will see a simple question. We have to find all numbers in the given list that are palindromes in nature. The method is simple, take each number from the list and check if it is a palindrome, then print that 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

The above is the detailed content of What are all the palindromic numbers in the list?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete