Computer Applications

Write a Java Program to define a function which accept a number and check it is a prime number or not

Write a Java Program to define a function which accept a number and check it is a prime number or not.

import java.io.*;
import java.util.*;

class Prime
{
	void check( int n )
	{
		int c = 0;
		for( int i = 1; i<=n ; i++)
                {
                       if( n%i == 0)
                            c = c + 1 ;
                }
		if( c == 2)
		  System.out.println(" Number is Prime.");
		else
		  System.out.println(" Number is not Prime.");
	}
  public static void main(String args[ ])
  {
  	Scanner sc = new Scanner(System.in);
  	int n;
  	System.out.println("Enter n =");
  	n = sc.nextInt();
  	Prime ob = new Prime( );
  	ob.check( n );
  }
}

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

To Top