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

mongs_Develop·2022년 4월 27일
0

Programmers-Level1-Java

목록 보기
14/30
post-thumbnail
  • 문제 & 예시

  • 소스코드

// 자릿수 더하기
public class test14 {
	public static void main(String[] args) {
		Solution14 sol = new Solution14();
		int n = 123;
		System.out.println(sol.solution(n));
	}
}
class Solution14 {
    public int solution(int n) {
        int answer = 0;
        int temp = 0;
        
        while(n>0) {       	// 예시
        	temp = n % 10;  // 3 
        	n = n / 10;     // 12
        	answer += temp;
        }

        return answer;
    }
}
  • consol
profile
개 발 인생

0개의 댓글