Baekjoon Algorithm | Problem 11720: Sum of Digits

Source

https://www.acmicpc.net/problem/11720

Problem

Solve Baekjoon Online Judge problem 11720, Sum of Digits.

Input

Follow the input format and constraints given in the original problem statement.

Output

Print the answer required by the problem.

Sample Input 1

1 1

Sample Output 1

1

Sample Input 2

5 54321

Sample Output 2

15

Sample Input 3

25 7000000000000000000000000

Sample Output 3

7

Sample Input 4

11

Sample Output 4

46

Algorithm Classification

  • Math
  • Implementation
  • Geometry

Solution

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {

        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
            final int n = Integer.parseInt(br.readLine());
            final String nums = br.readLine();

            int sum = 0;
            for (int i = 0; i < n; i++) {
                sum += nums.charAt(i) - 48;
            }

            System.out.println(sum);
        }
    }
}