Write a program to accept n numbers in a single dimension array. Print & count the Palindrome numbers from it.
Answers
Answer: Java:-
Explanation:
// C++ implementation of the approach
#include<bits/stdc++.h>
using namespace std;
// Function to return the reverse of n
int reverse(int n)
{
int rev = 0;
while (n > 0)
{
int d = n % 10;
rev = rev * 10 + d;
n = n / 10;
}
return rev;
}
// Function that returns true
// if n is a palindrome
bool isPalin(int n)
{
return (n == reverse(n));
}
// Function to return the
// count of digits of n
int countDigits(int n)
{
int c = 0;
while (n > 0)
{
n = n / 10;
c++;
}
return c;
}
// Function to return the count of digits
// in all the palindromic numbers of arr[]
int countPalinDigits(int arr[], int n)
{
int s = 0;
for (int i = 0; i < n; i++)
{
// If arr[i] is a one digit number
// or it is a palindrome
if (arr[i] < 10 || isPalin(arr[i]))
{
s += countDigits(arr[i]);
}
}
return s;
}
// Driver code
int main()
{
int arr[] = { 121, 56, 434 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << (countPalinDigits(arr, n));
return 0;
}
// This code is contributed by mits