728x90
반응형
728x90
반응형

소수 구하기 문제 입니다.

https://www.acmicpc.net/problem/1929

 

먼저 소수에 대한 개념을 알아야겠죠?

소수는 약수가 1과 자기 자신 뿐인 수인데요

어떻게 구해야 할까??

자기 자신보다 작은 수로 나누어, 하나라도 나눠진다면 소수가 아니겠죠

저는 나머지가 0인 경우와 분자와 분모가 같다면 소수라 가정하여 코드를 짰습니다.

 

 

import java.util.Scanner;

public class test {
	public static void main(String[] args) {
		System.out.print("수를 입력 : ");
		Scanner sc = new Scanner(System.in);
		int scInt = sc.nextInt();
		System.out.print("수를 입력 : ");
		int scInt2 = sc.nextInt();
		for(int i=scInt; i<=scInt2; i++){
			for(int j=2; j<=i; j++){
				if(i%j==0){
					if(i==j){
						System.out.print(i+" ");
					}else break;
				}
			}
		}
		System.out.println();
		System.out.println(scInt+"이상 "+scInt2+"이하의 소수입니다.");
	}
	
}

 

실행 결과입니다!

 

 

728x90
반응형
728x90
반응형

+ Recent posts