Using a for loop, determine whether the integer variable max is prime or not. If the number is prime, print out "prime". If it's not, print out "not prime". Keep in mind that 1 is NOT a prime number.
You can use the following to find the square root of a number (you may or may not use this):
Math.sqrt(num)
Sample input/outputs:
In: 100 out: not prime
In: 17 out: prime
In: 9 out: not prime
In: 1 out: not prime |
Solution
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
|
class Bakjoon {
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
System.out.print("In:");
double max = inp.nextDouble();
//write your code below
int sw=0;
if(max==1||max==2) {
System.out.println(max==2?"Prime":"not Prime");
}
else {
if(max%2==0) {
System.out.println("not Prime");
}
else {
if(max%i==0) {
sw=1;
}
}
if(sw==1) {
System.out.println("not Prime");
}
else {
System.out.println("Prime");
}
}
}
}
}
|
반응형
'Java_beginner(Repl.it) > Auto-Graded-Course(AP CS A)' 카테고리의 다른 글
026 - Further For Loop Practice 2 (skipping by 3s) (0) | 2019.12.07 |
---|---|
025 - Further For Loop Practice 1 (ascending) (0) | 2019.12.07 |
023 - For Loops 5 (0) | 2019.11.30 |
022 - For Loops 4 (0) | 2019.11.30 |
021 - For Loops 3 (0) | 2019.11.30 |