004-008-007

Varargs: Sum Calculation Method

Medium

Problem Description

Varargs: Sum Calculation method

In this problem, you will implement a calculateSum method using varargs (int... numbers) to accept any number of integers and compute their sum. Read multiple lines from standard input and output the sum for each line.

Learning Objective: Create a method that accepts a flexible number of arguments using varargs

Overview

Create a method calculateSum that accepts any number of integers and returns their sum. Implement using varargs.

Input Format

Multiple lines of input are given. Each line contains one or more space-separated integers.

10 20 30
5 15
100

Specifications

  • Method name: calculateSum
  • Parameter: varargs int... numbers
  • Return value: sum of all numbers (int type)
  • For each line, read the integers, pass them to calculateSum, and print the result

Output Format

Output the sum for each line in the following format.

Sum: 60
Sum: 20
Sum: 100

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