[알고리즘/프로그래머스] 자릿수 더하기

JudyLia·2022년 2월 12일
0

알고리즘

목록 보기
36/61
post-thumbnail

문제) 자연수 N이 주어지면, N의 각 자릿수의 합을 구해서 return 하는 solution 함수를 만들어 주세요.
예를들어 N = 123이면 1 + 2 + 3 = 6을 return 하면 됩니다.

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

        String s= n+"";
        String[] s2= s.split("");
        for(int i=0;i<s2.length;i++){
            answer+=Integer.parseInt(s2[i]);
        }

        return answer;
    }
}
profile
안녕:)

0개의 댓글

관련 채용 정보