Wednesday, 2 December 2020

Palindrome using Array

Write a Java Program to Check Whether a Array elements are Palindrome or Not  
import java.util.Scanner;
public class PalindromeArray 
{
public static void main(String[] args) 	
{
	Scanner scan = new Scanner(System.in);
	System.out.println("\nEnter how many nos u have to check:");
	int size=scan.nextInt();
	int numbers[] =new int[size];
	System.out.println("\nEnter Array of numbers to be checked");
	for(int i=0;i<size;i++)	
	{
		numbers[i] = scan.nextInt();
	}
	for(int i=0;i&ltnumbers.length; i++)	
	{
		int n1 = numbers[i];
		int reverseN1 = 0;
		int temp=0;				
		while(n1>0)	
		{
			temp =n1%10;
			n1=n1/10;
			reverseN1=reverseN1*10+temp;
		}
		if(numbers[i]==reverseN1) 
		{
		System.out.println("\n"+numbers[i]+ "  is pallindrome" );
		}
		else 
		{
		System.out.println("\n"+numbers[i]+ "  is  not a pallindrome");
		}
	}
}
}

Output
Enter how many nos u have to check:
5

Enter Array of numbers to be checked
123
4554
78987
951159
357357

123  is  not a pallindrome

4554  is pallindrome

78987  is pallindrome

951159  is pallindrome

357357  is  not a pallindrome


No comments:

Post a Comment

If you have any doubts, please let me know

Pattern 31 & 32