140k views
5 votes
Write a Java method to find the smallest number among six numbers.

2 Answers

4 votes

import java.util.Arrays;

public class SmallestIntArray{

public static int getSmallest(int[] a){

Arrays.sort(a);

return a[0];

}

public static void main(String []args){

int a[] = {1,2,3,4,5,0,1};

System.out.println(getSmallest(a));

}

}

The method getSmallest takes one arguement, an array of integers. The integers are then sorted in ascending order (smallest number first) then we return the first integer in the array.

import java.util.Arrays;

public class SmallestIntArray{

public static int getSmallest(int a, int b, int c, int d, int e, int f){

int ar[] = {a, b, c, d, e, f};

Arrays.sort(ar);

return ar[0];

}

public static void main(String []args){

System.out.println(getSmallest(1,2,3,4,5,0));

}

}

The method getSmallest takes 6 integers as arguements and then puts them into an array. We then sort that array in ascending order and we get the smallest number by returning the first integer in the array.

User Bernieslearnings
by
3.4k points
3 votes

Answer:

1. import java.util.*;

2. public class SmallestInArrayExample1{

3. public static int getSmallest(int[] a, int total){

4. Arrays.sort(a);

5. return a[0];

6. }

7. public static void main(String args[]){

8. int a[]={1,2,5,6,3,2};

Step-by-step explanation:

Here is some coding information.

User Leana
by
4.6k points