JAVA

(JAVA) 프로그래머스 하샤드 수 - LEVEL1

momo0503 2022. 9. 28. 19:38

class Solution {
    public boolean solution(int x) {
        boolean answer = true;
        
        int sum = 0; 
        
        String str = Integer.toString(x);

        for(int i =0;i<str.length();i++){
            sum +=  Integer.parseInt(str.substring(i,i+1));    
        }
        
        if(x%sum == 0){
            answer = true;
        }else{
            answer = false;
        }
        
        
        return answer;
    }
}

 

- 각자리 수의 합 sum 을 구하고, 입력값과 sum 나머지값으로 확인한다.