Coding Practice

Write a Java program to check whether a number is Prime number or not

Check whether a number is Prime number or not
Sample Output
Check whether a number is Prime number or not.

Enter a number: 7
7 is prime a number.
Java-Source Code
//package loopinjava;

import java.util.Scanner;

public class Main26 {

    public static void main(String[] args) {

        int n, number, isPrime = 1;

        Scanner input = new Scanner(System.in);

        System.out.println("Check whether a number is Prime number or not.\n");

        System.out.print("Enter a number: ");
        number = input.nextInt();

        for (n = 2; n < number; n++) {
            
            if (number % n == 0) {
                isPrime = 0;
            }
        }
        if (isPrime == 1) {
            
            System.out.println(number + " is prime a number.\n");
            
        } else {
            
            System.out.println(number + " is prime not a number.\n");
        }
    }
}
Sample Output
Check whether a number is Prime number or not.

Enter a number: 21
21 is prime not a number.

No comments:

Post a Comment

Change Theme
X