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.

Simple-Music-Player-App-(Version-2)-for-Android-(Offline-Version)
Simple Music Player App (Version 2) for Android (Offline Version)
HD-Wallpaper-App-in-Android-Studio-----------
HD Wallpaper App in Android Studio
Login-and-Register-using-Firebase-Realtime-Database
Login and Register using Firebase Realtime Database

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.