-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckPrimeFunction.java
50 lines (45 loc) · 1 KB
/
CheckPrimeFunction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.test.done;
import java.util.Scanner;
public class CheckPrimeFunction {
public static void main(String args[]) {
int num;
Scanner s = new Scanner(System.in);
num = s.nextInt();
prime(num);
printAllPrimes(num);
s.close();
}
public static void prime(int num) {
boolean isPrime = true;
if (num <= 1) {
isPrime = false;
} else {
for (int i = 2; i < num; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
System.out.println(num + " " + "is prime number");
} else {
System.out.println(num + " " + "is Composite number");
}
}
public static void printAllPrimes(int num) {
System.out.println("Prime numbers from 2 to " + num + ":");
for (int i = 2; i <= num; i++) {
boolean isPrime = true;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.println(i + " is a prime number");
}
}
}
}