Home > Backend Development > C++ > body text

What are all the palindromic numbers in the list?

王林
Release: 2023-09-10 11:25:02
forward
1202 people have browsed it

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
Copy after login

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);
}
Copy after login

Output

All palindromes: 121 111 858
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!