본문 바로가기
(문제풀이)

프로그래머스(java) : 자릿수 더하기

by cogito-new 2022. 10. 10.

class Solution {
    public int solution(int n) {
        int answer = 0;
        
        while (n !=0 ) {
            if(n >=10) {
                answer += n%10;
                n = n/10;
            } else {
                answer += n;
                break;
            }
        }
        return answer;
    }
}

 

반응형