[CodingTest] Java : Sum of Numbers
Solving Problem 11720: Sum of NumbersPermalink
Problem DescriptionPermalink
You are given N numbers written consecutively without spaces. Write a program to calculate and print the sum of these numbers.
ConstraintsPermalink
- 1 <= N <= 100
- The input numbers are provided without spaces.
Input FormatPermalink
- The first line contains an integer N, the number of digits.
- The second line contains N consecutive digits without spaces.
Output FormatPermalink
- Print the sum of the given digits.
ExamplesPermalink
Example 1Permalink
Input:
1
1
Output:
1
Example 2Permalink
Input:
5
54321
Output:
15
Example 3Permalink
Input:
25
7000000000000000000000000
Output:
7
Example 4Permalink
Input:
11
10987654321
Output:
46
ApproachPermalink
This problem is solved by iterating through each digit of the input string, converting it to an integer, and adding it to a running sum. The approach is straightforward and efficient given the constraint.
Steps to Solve:Permalink
- Read the input values.
- Convert the string of numbers into a character array.
- Loop through each character, convert it to its numeric value, and add it to the sum.
- Print the total sum.
Java ImplementationPermalink
package datastruc.R1;
import java.util.*;
public class P11720 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
String sNum = sc.next();
// Convert string to char array
char cNum[] = sNum.toCharArray();
int sum = 0;
// Sum each digit
for (int i = 0; i < N; i++) {
sum += cNum[i] - '0'; // Convert char to int by subtracting '0'
}
System.out.println(sum);
sc.close();
}
}
Explanation of the CodePermalink
- Input Handling:
Scanner
is used to read input.nextInt()
reads the number of digits ( N ).next()
reads the string of ( N ) digits.
- Character Array Conversion:
- The
toCharArray()
method converts the string into an array of characters, where each character represents a single digit.
- The
- Summation Loop:
- Iterate through each character in the array.
- Convert the character to its integer value by subtracting
'0'
(ASCII adjustment). - Add the integer value to the running total.
- Output:
- Print the total sum.
LinksPermalink
- Problem Link: Baekjoon 11720
- Source Code: GitHub Repository
SummaryPermalink
This solution effectively handles the constraints and uses basic string and array manipulation techniques in Java to solve the problem. The time complexity is ( O(N) ), making it efficient given the problem’s limits.
Leave a comment