본문 바로가기

프로그래머스

프로그래머스 - 자릿수 더하기

public class Solution {
public int solution(int n) {
int answer = 0;

String strN = String.valueOf(n);
int c;

for (int i = 0; i < strN.length(); i++) {
c = strN.charAt(i) - 48;
answer += c;
}

return answer;
}
}