1// Prime number program in java using scanner
2import java.util.Scanner;
3public class PrimeNumberUsingScanner
4{
5 public static void main(String[] args)
6 {
7 Scanner sc = new Scanner(System.in);
8 System.out.println("Please enter a number to check prime number: ");
9 int number = sc.nextInt();
10 if(checkPrimeNumber(number))
11 {
12 System.out.println(number + " is a prime number");
13 }
14 else
15 {
16 System.out.println(number + " is not a prime number");
17 }
18 sc.close();
19 }
20 public static boolean checkPrimeNumber(int number)
21 {
22 if(number <= 1)
23 {
24 return false;
25 }
26 for(int a = 2; a < Math.sqrt(number); a++)
27 {
28 if(number % a == 0)
29 {
30 return false;
31 }
32 }
33 return true;
34 }
35}
1public class Prime {
2 public static void main(String[] args) {
3
4 int num = 29;
5 boolean flag = false;
6 for(int i = 2; i <= num/2; ++i){
7 if(num % i == 0){
8 flag = true;
9 break;
10 }
11 }
12
13 if (!flag)
14 System.out.println(num + " is a prime number.");
15 else
16 System.out.println(num + " is not a prime number.");
17}}
1public class Prime {
2 public static void main(String[] args) {
3
4 int num = 29;
5 boolean flag = false;
6 for(int i = 2; i <= num/2; ++i){
7 if(num % i == 0){
8 flag = true;
9 break;
10 }
11 }
12
13 if (!flag)
14 System.out.println(num + " is a prime number.");
15 else
16 System.out.println(num + " is not a prime number.");
17}}
18Source:www.progra
19