Computer Science, asked by Himanshukatoch8756, 6 months ago

Program of bluej to check prime or not

Answers

Answered by nafeess2019
1

With Scanner class

import java.util.*;

public class Prime

{

   public static void main(String[]args)

   {

       Scanner scan = new Scanner(System.in);

       System.out.println("Enter a Number :");

       int n= scan.nextInt();

       int i,c=0;

       for(i=1;i<=n;i++)

       {

           if(n%i==0)

           c=c+1;

       }

       if(c==2)

       {

           System.out.println(n+" is a prime number");

       }

       else

       {

           System.out.println(n+" is not a prime number");

       }

   }

}

Without Sccaner class

public class Prime

{

   public static void main(int n)

   {

       int i,c=0;

       for(i=1;i<=n;i++)

       {

           if(n%i==0)

           c=c+1;

       }

       if(c==2)

       {

           System.out.println(n+" is a prime number");

       }

       else

       {

           System.out.println(n+" is not a prime number");

       }

   }

}

Note: initialize n in the method call

Mark me the Brainliest

Similar questions