Monday 5 January 2015

Java Program to check whether a number is Prime Or Composite


This is a basic program in Java which checks whether the number is prime number or composite number.

PROGRAM :


 package codingcorner.in;

import java.util.Scanner;

public class PrimrOrComposite {
public static void main(String[] args) {
int a, b;
Scanner scan = new Scanner(System.in);

System.out.println("Enter a number to check Prime or Composite ");
b = scan.nextInt();

scan.close();

if (b == 0 || b == 1)
System.out.println(b +" is neither Prime nor Composite" );

for (a = 2; a <= b - 1; a++) {
if (b % a == 0) {
System.out.println(b +" is Composite number" );
break;
}
}
if (b == a)
System.out.println(b +" is Prime number " );

}
}
OUTPUT :



1 comment:

  1. Good but I want to write this program using switch statement

    ReplyDelete