//This program will demonstate function with variable arguments.
//Here there are two function to achieve it. One method accepts the values in array from keyboard and perform sum of all values.
//Another method simply accepts all value with java variable numbers of argument concept and perform sum of all values.
import java.util.*;
class VarArgs{
public static void main(String a[]){
VarArgs va=new VarArgs();
System.out.println("Enter how many inputs do you want to enter :");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] vars=new int [n];
System.out.println("Enter " +n +"numbers to add");
for(int i=0;i<n;i++){
vars[i]=sc.nextInt();
}
for(int v:vars){
System.out.println("Entered values are :"+v);
}
va.sum(10,20,30);
va.sum(50,90);
va.sum(10,20,30,40,50,60);
va.sumArray(vars);
}
//Method which accepts variable arguments
void sum(int... inputs){
int x=0;
for(int value:inputs){
x+=value;
}
System.out.println("Sum of all numbers with variable number of arguments is:" + x);
}
//Methods which accept arry as it's arument
void sumArray(int arr[]){
int x=0;
for(int value:arr){
x+=value;
}
System.out.println("Sum of all numbers with array is :" + x);
}
}
Out put:
Enter how many inputs do you want to enter :
4
Enter 4numbers to add
12
23
34
45
Entered values are :12
Entered values are :23
Entered values are :34
Entered values are :45
Sum of all numbers with variable number of arguments is:60
Sum of all numbers with variable number of arguments is:140
Sum of all numbers with variable number of arguments is:210
Sum of all numbers with array is :114
//Here there are two function to achieve it. One method accepts the values in array from keyboard and perform sum of all values.
//Another method simply accepts all value with java variable numbers of argument concept and perform sum of all values.
import java.util.*;
class VarArgs{
public static void main(String a[]){
VarArgs va=new VarArgs();
System.out.println("Enter how many inputs do you want to enter :");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] vars=new int [n];
System.out.println("Enter " +n +"numbers to add");
for(int i=0;i<n;i++){
vars[i]=sc.nextInt();
}
for(int v:vars){
System.out.println("Entered values are :"+v);
}
va.sum(10,20,30);
va.sum(50,90);
va.sum(10,20,30,40,50,60);
va.sumArray(vars);
}
//Method which accepts variable arguments
void sum(int... inputs){
int x=0;
for(int value:inputs){
x+=value;
}
System.out.println("Sum of all numbers with variable number of arguments is:" + x);
}
//Methods which accept arry as it's arument
void sumArray(int arr[]){
int x=0;
for(int value:arr){
x+=value;
}
System.out.println("Sum of all numbers with array is :" + x);
}
}
Out put:
Enter how many inputs do you want to enter :
4
Enter 4numbers to add
12
23
34
45
Entered values are :12
Entered values are :23
Entered values are :34
Entered values are :45
Sum of all numbers with variable number of arguments is:60
Sum of all numbers with variable number of arguments is:140
Sum of all numbers with variable number of arguments is:210
Sum of all numbers with array is :114
No comments:
Post a Comment