알고리즘
[Java] SWEA - 자릿수 더하기
J3SUNG
2023. 1. 12. 21:11
728x90
나누기와 모듈러 연산을 통해서 각자리를 전부 더한 후 출력해주었습니다.
import java.util.Scanner;
import java.io.FileInputStream;
class Solution
{
public static void main(String args[]) throws Exception
{
Scanner sc = new Scanner(System.in);
int ans = 0;
int n = sc.nextInt();
while(n != 0){
ans += n % 10;
n /= 10;
}
System.out.printf("%d", ans);
}
}