본문 바로가기
알고리즘/Java

소수 구별하기(Java)

by 싸공 2020. 7. 10.

 

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
import java.util.Scanner;
 
public class Primenumber {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int input = 0;
        int i = 0;
        int j = 2;
        boolean flag = true;
            input = sc.nextInt();
            i = input - 1;
            if (j <= i){
                if (input % j == 0){
                    flag = false;
                }
            }
            else{
                flag = true;        
            }
            if(flag == true)
                System.out.println("소수입니다.");
            else
                System.out.println("소수가 아닙니다.");
        }
    }
 
cs

 

출력 화면