Saturday, September 2, 2017

Find minimum integer from an array in Java

Problem: Write a program to find a minimum value from an array.



1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package arrays;
import java.lang.reflect.Array;
import java.util.*;
/**
 *
 * @author madhav
 */
public class MinArrays {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner sc = new Scanner(System.in);
        int[] myArray = new int[15];
        int value = 0;
        int n = 0;
        System.out.println("Please enter a number, -1 to exit");
        while(value != -1 && n < 15){
            value = sc.nextInt();
            sc.nextLine();
            if(value != -1){
                myArray[n] = value;
                n++;
            }
        }
        
        if(n == 0)
            System.out.println("You did not enter any number! Byee..");
        else {
            int minimum = myArray[0];
            for(int i = 1; i< n; i++){
                if(myArray[i] < minimum)
                    minimum = myArray[i];
            }
            System.out.println("Minimum value is:" + minimum);
        }
    }
}






No comments:

Post a Comment

My Journey from a Tier-3 College to Microsoft, Google, and Meta: Lessons Learned

Original post: Link   Time to give back to community. Went through couple of post myself and got inspired and belief that cracking FAANG is ...