13) Write a java Program to accept ‘n’ no’s through the command line and store all the prime no’s and perfect no’s into the different arrays and display both the arrays.


import java.io.*;
class ArrayDemo2
{
public static void main (String args[]) throws IOException
{
int j,s,i,k=0,m=0,n;
int r[]=new int[10] ;
int prime[]=new int[10] ;
int perfect[]=new int[10] ;
BufferedReader br=new BufferedReader(new InputStreamReader (System.in));
System.out.println(“Enter the Number Of Elements :=> “);
n=Integer.parseInt(br.readLine());
for (i=0;i<n;i++)
{
;
System.out.println(“Enter the “+i+” Number of the Array :=> “);
r[i]= Integer.parseInt(br.readLine());
}

for(j=0;j<10;j++)
{
int flag=0;
if(r[j]==0||r[j]==1)
{
continue;
}
else
{
for(i=2;i<r[j];i++)
{
if(r[j]%i==0)
flag=1;
}
}
if(flag==0)
{
prime[k]=r[j];
k++;
}
s=0;
for(i=1;i<r[j];i++)
{
n=r[j]%i;

if(n==0)
s=s+i;
}
if(s==r[j])
{
perfect[m]=r[j];
m++;
}

}
System.out.println(“\nPerfect Numbers are “);
for(i=0;i<m;i++)
{
System.out.print(perfect[i]+” “);
}

System.out.println(“\nPrime Numbers are”);
for(i=0;i<k;i++)
{
System.out.print(prime[i]+” “);
}

}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s