Java Examples

100+ tutorials, source code, and examples to help you develop creative and technical skills.

Java Program to check Prime Number

PrimeNumbers.java

import java.util.Scanner;

/* Code From Learnoset - Learn Coding Online.
    Download Learnoset App from PlayStore to learn coding,
    projects, algorithms, error handling
 */
 
 // Java Program to check Prime Number
 
public class PrimeNumbers {
    
	public static void main(String args[]) {
        
		boolean isPrimeValue = true;
		
		// create Scanner class object to get input from user
        Scanner scanner = new Scanner(System.in);
		
        System.out.println("Enter a number:");
		
        //get user's enetered value from 'scanner' object into 'num' variable
        int num = scanner.nextInt();
		
		int halfNum = num / 2;		
      
        if (num == 0 || num == 1) {
            System.out.println(num + " is not a prime number");
        } else {
            for (int i = 2; i <= halfNum; i++) {
                if (num % i == 0) {
                    System.out.println(num + " is not a prime number");
                    isPrimeValue = false;
                    break;
                }
            }
            if (isPrimeValue) {
                System.out.println(num + " is a prime number");
            }
        } 
    }
}

Projects with Source Code + Video Tutorials

You can download our Java and Android Studio Projects with Source Code and Video Tutorials.

Modern-Login-and-Register-screen-UI-design
Modern Login and Register screen UI design
Online-Quiz-Application-using-Firebase-Realtime-Database-Source-Code
Online Quiz Application using Firebase Realtime Database Source Code
Settings-and-Profile-page-UI-design-in-Android-Studio-with-Example-and-Source-Code
Settings and Profile page UI design in Android Studio with Example and Source Code

If you have any Questions or Queries
You can mail us at info.learnoset@gmail.com

Follow us to learn Coding and get in touch with new Technologies.