Number is prime or not in Java

A prime number is a whole number greater than 1. It has exactly two factors, that is, 1 and the number itself. Using for the for loop and if condition statement. A for loop is a repetition control structure which allows us to write a loop that is executed a specific number of times. The loop enables us to perform n number of steps together in one line. The if statement is the most basic of all the control flow statements. It tells your program to execute a certain section of code only if a particular test evaluates to true. The if statement is written with the if keyword.

Source Code

import java.util.Scanner;
public class prime {
    //Write a program to check the given number is prime or not.
    public static void main(String[] args) {
        Scanner in = new Scanner(;
        System.out.print("Enter The Number : ");
        int n = in.nextInt();
        int f = 0;
        for (int i = 1; i <= n; i++) {
            if (n % i == 0) {
        if (f == 2) {
            System.out.println(n + " is a Prime Number");
        } else {
            System.out.println(n + " is not a Prime Number");


Enter The Number : 17
17 is a Prime Number
Enter The Number : 10
10 is not a Prime Number
To download raw file Click Here

Basic Programs