Prime Number Program in Java
Prime Number Program in Java: The prime number is a natural number who value is greater than 1 and can be divisible by 1 and by itself. The number of counts to find the prime numbers is 2 because it can divisible only by 1 and by itself For example: 6 - can be divisible by 1x6,2x3,3x2,6x1. where the count is 4 and it is not a prime number 7 - can be divisible by 1x7,7x1. where the count is 2 and it is a prime number Example 1: Program to check whether the given number is prime number or not import java.util.Scanner; public class Primenumbers { public static void main(String[] args) { int i =2; boolean flag = true; System.out.print("Enter the number to find it is prime or not: "); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); if(n<0) { System.out.println("The prime number cannot be negative"); } else { while(i<n) { if(n%i==0) { flag = false; break; } i++;