006-003-009

Sum from 1 to N with a while Loop

Easy

Problem Description

Sum from 1 to N with a while loop

In this problem, you will create a program that reads an integer N from standard input and uses a while loop to accumulate integers from 1 to N, then displays the total sum to standard output.

Learning Objective: Understand basic looping with while statements

Overview

Create a program that reads N and computes the sum of integers from 1 to N using a while loop.

Specifications

  1. Read integer N from standard input (one line)
  2. Define sumUpTo(int n) method
  3. Use a while loop to compute and return the sum from 1 to n
  4. In main, print the result as Sum(1..N): total

Input Format

N

Output Format

Sum(1..N): total

Example

Input:  10
Output: Sum(1..10): 55

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up