Showing posts with label prime number check. Show all posts
Showing posts with label prime number check. Show all posts

Saturday, 14 May 2016

Java code to check whether number is prime or composite

import java.util.Scanner;
public class CheckPrime {
   
 
    public static void main(String args[])
    {
    Scanner ob= new Scanner(System.in);
    int a,i;
    System.out.println("Enter an integer");
    a=ob.nextInt();
    if(a==1)
        System.out.println("Number is prime");
    for(i=2;i<a;i++)
    {
     if(a%i==0) 
     {System.out.println("Number is composite");
     break;}
   
    }
    if(i==a)
        System.out.println("Number is prime");
   

}}