Menu Close

Java program number prime or not …FTC


Java program to check number for prime or not. If the number is Prime print “yes” else print “no”

Input:  1

Output:  Yes, its a prime number
import java.io.*;
import java.util.*;
public class temp {
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		System.out.println("Enter the Number:  ");
		int num = scan.nextInt();
		for(int i = 2 ; i< num; i++){
		if (num%i == 0){
			System.out.println("NO");
			return;
		}
	}
	System.out.print("YES");
}
}

INPUT_1:
Enter the Number:
25

OUTPUT:
NO


INPUT_2:
Enter the Number:
7

OUTPUT:
YES


INPUT_3:
Enter the Number:
77

OUTPUT:
NO


INPUT_4:
Enter the Number:
277

OUTPUT:
YES


INPUT_5:
Enter the Number:
211

OUTPUT:
YES


INPUT_6:
Enter the Number:
123

OUTPUT:
NO


ILLUSTRATION

Executed using javac in linux terminal, to check out whether the given number is prime or not.