[SWEA] D1.자릿수 더하기 (C++)

jhyunn·2024년 2월 28일
0

SWEA

목록 보기
6/12

2058. 자릿수 더하기

문제 링크

#include <iostream>
 
using namespace std;
 
int main() {
    int N, ans = 0;
    cin >> N;
    while (N > 0) {
        ans += N % 10;
        N /= 10;
    }
    cout << ans;
}

 
풀이
loop로 10씩 나누면서 나머지를 누적 합산한다.

profile
https://github.com/Sungjeonghyun

0개의 댓글