글
프로그래머스 C# 자릿수 더하기(while문, P, J)
프로그래밍
2023. 1. 22. 23:18
728x90
SMALL
using System;
public class Solution {
public int solution(int n) {
int answer = 0;
while(n >= 10)
{
answer += n%10;
n /= 10;
}
answer += n%10;
return answer;
}
}
파이썬
def solution(n):
return sum(int(i) for i in str(n))
자바
class Solution {
public int solution(int n) {
int answer = 0;
while(n>0)
{
answer+=n%10;
n/=10;
}
return answer;
}
}
728x90
'프로그래밍' 카테고리의 다른 글
프로그래머스 C# 정수 제곱근 판별(for i*i, sqrt) (0) | 2023.01.23 |
---|---|
프로그래머스 C# 자연수 뒤집어 배열로 만들기(for, long) (0) | 2023.01.22 |
프로그래머스 C# 약수의 합(int형으로 가능) (0) | 2023.01.22 |
프로그래머스 C# 소수 찾기(제곱근, 루트, 2~n, 2~root(n)) (0) | 2023.01.22 |
프로그래머스 C# 문자열 내 마음대로 정렬하기(string.OrderBy(), Linq) (0) | 2023.01.22 |