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.
Bottom Navigation Bar with Fragments + Animation
Create your own Web Browser Source Code
Login and Register screen with OTP Verification Material UI design
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.