25/12/2014

Increase Number of Arguments Without Changing the Parameters of Function in Java

This is the simple but logical Java program for beginners. The program modifies the number of arguments without changing the parameters of function in Java.

Source Code


public class MyMain {

 public static void main(String[] args) {
  System.out.print(sumAll(1,2)+"\n");
  System.out.print(sumAll(1,2,3)+"\n");
  System.out.print(sumAll(1,2,3,4)+"\n");
  System.out.print(sumAll(1,2,3,4,5)+"\n");
  
 }
 // which takes any number of integers and returns their sum
 public static int sumAll(int...numbers){
  int result = 0;
  for(int i = 0 ; i<numbers.length;i++)
  {
   result+=numbers[i];
  } 
  return result;
 }
}

Output of the Program

Parameters Logic program in Java

No comments:

Post a Comment