write a program to create a single dimensions array of n integer print only those integer from the array which are palindrome numbers java
Answers
Answer:
import java.util.Scanner;
public class Main
{
static boolean isPalindrome(int n){
int rev = 0;
int temp = n;
while(temp!=0){
int last = temp%10;
rev = rev*10+last;
temp/=10;
}
if(rev == n){
return true;
}
return false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many numbers in the array:");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter all " + n + " elements in the array");
for(int i = 0; i < n; i++){
arr[i] = sc.nextInt();
}
System.out.println("ORIGINAL ARRAY-");
for(int i = 0; i < n; i++){
System.out.print(arr[i] + " ");
}
System.out.println("\nPALINDROMES IN THE ARRAY");
for(int i = 0; i < n; i++){
if(isPalindrome(arr[i])){
System.out.print(arr[i] + " ");
}
}
}
}
Explanation: