004-005-002
While Loop: Sum Calculation
Easy
Problem Description
while loop: Sum Calculation
In this problem, you will create a program that uses a while loop to accumulate the sum of integers from 1 to a specified maximum value and displays the result to standard output.
Learning Objective: Implement accumulation processing with while loop
Calculate sum from 1 to specified number. Use while loop for cumulative addition.
Input
Line 1: Maximum value (integer, 1 or greater)
Output
Sum from 1 to [max]: [sum]
```java
## Examples
### Example 1: Sum from 1 to 5
Input:
```java
5
```java
Output:
```java
Sum from 1 to 5: 15
```java
1+2+3+4+5=15
### Example 2: Sum from 1 to 10
Input:
```java
10
```java
Output:
```java
Sum from 1 to 10: 55
```java
1+2+...+10=55
### Example 3: Sum from 1 to 1 (Boundary)
Input:
```java
1
```java
Output:
```java
Sum from 1 to 1: 1
Test Cases
※ Output examples follow programming industry standards
Normal case
Input:
3
Expected Output:
Sum from 1 to 3: 6
Normal case
Input:
7
Expected Output:
Sum from 1 to 7: 28
Your Solution
Current Mode:● My Code
99
1
2
3
4
5
6
7
8
9
10
›
⌄
⌄
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Write your code here
sc.close();
}
}
0 B / 5 MB
You have 10 free executions remaining
