프로그래머스코딩테스트연습풀이/C++

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

코코쵸마 2025. 2. 12. 17:41

문제

정수 n이 매개변수로 주어질 때 n의 각 자리 숫자의 합을 return하도록 solution 함수를 완성해주세요

 

솔루션

#include <string>
#include <vector>

using namespace std;

int solution(int n) {
    int answer = 0;
    while(1){
        answer += n%10;
        n /= 10;
        if(n==0){
            return answer;
        }
    }
    return answer;
}

 

https://school.programmers.co.kr/learn/courses/30/lessons/120906

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr