카테고리 없음

[프로그래머스, JAVA] 자릿수 더하기

yebin0322 2025. 3. 16. 21:28
반응형

https://school.programmers.co.kr/learn/courses/30/lessons/120906

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

class Solution {
    public int solution(int n) {
        //n의 각 자리 숫자의 합 return
        int answer = 0;
        String nStr = n + "";
        String[] arr = nStr.split("");
        for(int i=0; i<arr.length; i++){
            answer += Integer.parseInt(arr[i]);
        }
        return answer;
    }
}

 

 

반응형