004-008-007

Varargs: Sum Calculation Method

Medium

Problem Description

Varargs: Sum Calculation Method

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.

Specifications

  • Method name: calculateSum
  • Parameter: varargs int... numbers
  • Return value: sum of all numbers (int type)
  • Execute the following test cases in main method
    • calculateSum(10, 20, 30) → 60
    • calculateSum(5, 15) → 20
    • calculateSum(100) → 100

Output 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?