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

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment