Sunday, July 16, 2017

Write a Java method to find the smallest number among three numbers using arrays and methods.

/**
 * Created by Jo on 7/10/2017.
 * For more exercises and lessons, visit http://shegertech.blogspot.com/
 * Chapter 8, Exercise 1
 */

public class Q1Solution {

void smallestNumber(int[] arr){
    int smallestNumber =arr[0];
    for (int i=1;i< arr.length;i++){
        if (smallestNumber>arr[i]){
            smallestNumber=arr[i];
        }
    }
    System.out.println("The smallest number in tbe array is "+ smallestNumber);
}
    public static void main(String[] args) {

        int[] arr={55, 34, 11, 87, 29};
        Q1Solution q1 = new Q1Solution();
        q1.smallestNumber(arr);
     }
}

Share: