0% found this document useful (0 votes)
65 views

Java Problem 2 - Prime Numbers

This Java code takes in an array of integers as input, checks which numbers are prime and have their reverse also being prime, stores those numbers in a separate array, sorts that array, and prints out the sorted results. It defines methods to check if a number is prime and to reverse a number. The main method gets input, calls the prime checking and reversing methods, stores qualifying numbers in a new array, sorts that array, and prints out the sorted results.

Uploaded by

jenisha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Java Problem 2 - Prime Numbers

This Java code takes in an array of integers as input, checks which numbers are prime and have their reverse also being prime, stores those numbers in a separate array, sorts that array, and prints out the sorted results. It defines methods to check if a number is prime and to reverse a number. The main method gets input, calls the prime checking and reversing methods, stores qualifying numbers in a new array, sorts that array, and prints out the sorted results.

Uploaded by

jenisha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

*;
import java.io.*;
public class Solution
{
public static void main(String[] args)
{
int j=0,size=0;
Scanner sc=new Scanner(System.in);
//System.out.println("enter the value of size:");
size=sc.nextInt();
int a[]=new int[size];
int b[]=new int[size];
//System.out.println("enter the values:");
for(int i=0;i<a.length;i++)
a[i]=sc.nextInt();
for(int i=0;i<a.length;i++)
{

if(isPrime(a[i]))
{
int n=reverse1(a[i]);
if(isPrime(n)&&n!=1)
{
b[j++]=a[i];
}
}
}
for(int i=0;i<j;i++)
{
for(int k=i+1;k<j;k++)
{
if(b[i]>b[k])
{
int temp=b[i];
b[i]=b[k];
b[k]=temp;
}
}
}
for(int i=0;i<j;i++){//System.out.println("The values are:");
System.out.println(b[i]);}

}
public static boolean isPrime(int n)
{
int temp=2,i=2;
for(i=2;i<n;i++)
{
if(n%i==0)
temp++;
}
if(temp==2)
return true;
return false;
}

public static int reverse1(int n)


{
int r,res=0;
while(n>0)
{
r=n%10;
res=res*10+r;
n=n/10;
}
return res;
}
}

You might also like