[Programmers] 자릿수 더하기 - 연습문제

동민·2021년 3월 10일
// 자릿수 더하기 - 연습문제
public class AddEachNum {

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

		String str = Integer.toString(n);
		for (int i = 0; i < str.length(); i++) {
			answer += Integer.parseInt(str.charAt(i) + "");
		}

		return answer;
	}

	public static void main(String[] args) {
		AddEachNum s = new AddEachNum();
		System.out.println(s.solution(123));
		System.out.println(s.solution(987));
	}
}
profile
BE Developer

0개의 댓글