본문 바로가기
JAVA/Coding Test Study

[D1] SWEA - 2058. 자릿수 더하기 : Java

by ♡˖GYURI˖♡ 2023. 11. 18.
728x90

습관처럼 for을 쓰려다 while로 다시 작성했다

 

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 n = sc.nextInt();
            int sum = 0;
            while(n > 0) {
                sum += n % 10;
                n /= 10;
            }
            System.out.println(sum);
	}
}