Java Programing
Exercise #3:
Write a Java class that implements a static method –
SortNumbers(int… numbers) with variable number of arguments. The
method should be called with different numbers of parameters and
does arrange the numbers in descending order. Call the method
within main method of the driver classand display the results.
Java program:
class Main
{
static void SortNumbers(int ...numbers)
{
int arr[]=new int[numbers.length];
int q=0,temp;
for (int i: numbers){
arr[q]=i;
q++;
}
for (int i= 0;i<arr.length;i++) {
for (int j= i+1;j < arr.length;j++) {
if(arr[i]<arr[j]) {
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
}
public static void main(String args[])
{
SortNumbers(1,2,3,4,5,6);
}
}
if you like the answer please provide a thumbs upl
Get Answers For Free
Most questions answered within 1 hours.