005-001-003
Loops: Number Sum
Medium
Problem Description
Problem Overview
Create a program that inputs a positive integer N and calculates the sum from 1 to N.
Input Format
Receive 1 line of input:
- Positive integer N (1 or greater)
Output Format
Output in the following format:
Sum from 1 to [N]: [sum]
```java
## Example
Input: 5
Output: Sum from 1 to 5: 15
(Calculation: 1+2+3+4+5=15).
Test Cases
※ Output examples follow programming industry standards
Input:
5
Expected Output:
Sum from 1 to 5: 15
Input:
10
Expected Output:
Sum from 1 to 10: 55
Input:
1
Expected Output:
Sum from 1 to 1: 1
Input:
100
Expected Output:
Sum from 1 to 100: 5050
Input:
3
Expected Output:
Sum from 1 to 3: 6
❌ Some tests failed
Your Solution
Current Mode:● My Code
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
›
⌄
⌄
⌄
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 入力: N
int n = sc.nextInt();
// すべての値を加算して適切な式を使って合計結果を計算
int sum = 0;
// [のループ] 指定回数繰り返す
for (int i = 1; i <= n; i++) {
sum += i;
}
// 結果をユーザーにコンソール経由で表示
System.out.println("Sum from 1 to " + n + ": " + sum);
}
}
0 B / 5 MB
You have 10 free executions remaining
