코딩테스트/Level1

진약수의 합

MJ.Lee 2022. 2. 9. 08:59

문제

자연수 N이 주어지면 자연수 N의 진약수의 합을 수식과 함께 출력하는 프로그램을 작성하세
요.

입력 예제
20

출력 예제
1 + 2 + 4 + 5 + 10 = 22

public class problem03 {

	public static void main(String[] args) {
		//진약수의 합 
		//진약수: 자연수 n의 약수들 중에서 자기 자신인 n을 제외한 약수
		int A = 20;
		int result = method(A);
		System.out.println(result);
		
	}
	
	public static int method(int A) {
		
		int result = 1; //1은 모든 숫자의 약수
		for(int i=2; i< A; i++) {
			if(A%i == 0) {
				result += i;
				System.out.println(i);
			}
		}

		return result;
	}
	
}