JAVA program to check whether a given number is prime or not using method

JAVA program to check whether a given number is prime or not using method

This JAVA program is to check whether a given number is prime or not using method.

For example, 17 is a prime number.

Dry run of the program has been given here(click on the link) only additional part is the use of method.

As practice you can run the same program with the different ways of using methods.

If you yet need the dry run of the program or any other query, then kindly leave a comment in the comment box or mail me, I would be more than happy to help you.

Program

import java.util.*;

class mr1
{
	public static void main(String args[])
	{
    		int n,i;
		Scanner sc = new Scanner(System.in);
    		System.out.println("Enter any number");
    		n=sc.nextInt();
    		prime(n);
	}

	static void prime(int n)
	{
		int i,c=0;
    		for(i=1;i<= n;i++)
		{
       			if(n%i==0) 
       			{
           			c++;
        		}
    		}
		if(c==2)
    		{
  			System.out.println(n+" is a Prime number");
    		}
    		else 
    		{
 			System.out.println(n+" is not a Prime number");
    		}
	}
}

Output

Share Me!