자릿수 더하기
https://programmers.co.kr/learn/courses/30/lessons/12931
코딩테스트 연습 - 자릿수 더하기
자연수 N이 주어지면, N의 각 자릿수의 합을 구해서 return 하는 solution 함수를 만들어 주세요. 예를들어 N = 123이면 1 + 2 + 3 = 6을 return 하면 됩니다. 제한사항 N의 범위 : 100,000,000 이하의 자연수 입출
programmers.co.kr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int solution(int n) | |
{ | |
int answer = 0; | |
while(n > 0) | |
{ | |
int num = n % 10; | |
n /= 10; | |
answer += num; | |
} | |
return answer; | |
} |
반응형
'자료구조 와 알고리즘 > 프로그래머스' 카테고리의 다른 글
두 개 뽑아서 더하기 (0) | 2022.05.08 |
---|---|
이상한 문자 만들기 (0) | 2022.05.08 |
콜라츠 추측 (0) | 2022.05.08 |
평균 구하기 (0) | 2022.05.08 |
하샤드 수 (0) | 2022.05.08 |