BCA Slip26 Java

Java

Slip26

Q.1) 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. [Marks 30]

Solution

 import java.io.*;
class Primeno
{
  public static void main(String args[])
  {
int n=args.length;
int pr[]=new int[n];
int per[]=new int[n];
int k=0,i,j;
int sum=0;
//Prime No
for(i=0;i<n;i++)
{
          for(j=2;j<Integer.parseInt(args[i]);j++)
  {
    if(Integer.parseInt(args[i])%j==0)
break;
  }
  if(Integer.parseInt(args[i])==j)
  pr[k++]=Integer.parseInt(args[i]);
}

System.out.println("Prime Array = ");
for(j=0;j<k;j++)
{
  System.out.print("  "+pr[j]);
}

//Perfect No
k=0;
for(i=0;i<n;i++)
{
          sum=0;
  for(j=1;j<Integer.parseInt(args[i]);j++)
  {
     if(Integer.parseInt(args[i])%j==0)
     {
sum=sum+j;
     }
          }

if(sum==Integer.parseInt(args[i]))
{
        per[k++]=sum;
}
    }

    System.out.println("\nPerfect Array = ");
    for(i=0;i<k;i++)
    {
System.out.print("  "+per[i]);
    }
  }

}
BCA Pratical Solution

My name is Vivek And I from Mumbai and Complete my Graduation Bca.my Age is 23 Years.

Post a Comment

Previous Post Next Post

DBMS Practical Slips