Find the Minimum & Maximum Number in Java


 Java Tutorial

Find the Minimum and Maximum Number in Java

Source code:

import java.util.Scanner;

public class FindMinMaxNum {
    public static void main(String[] args) {
        System.out.println("Enter the size of arrays");
        Scanner sc = new Scanner(System.in);
        int size = sc.nextInt();
        int[] a = new int[size];

        // int max = a[0];
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;

        System.out.println("Enter the numbers");
        // Take input from user
        for (int i = 0; i < size; i++) {
            a[i] = sc.nextInt();
        }

        // Print maximun number
        for (int i = 0; i < size; i++) {
            if (a[i] > max) {
                max = a[i];
            }
        }
        System.out.println("max number is : " + max);

        // Print minimun number
        for (int i = 0; i < size; i++) {
            if (a[i] < min) {
                min = a[i];
            }
        }
        System.out.println("min number is : " + min);
    }
}


Descriptions: 

  • This tutorial shows you how to find the minimum and maximum number in Java. 
  • We will use a simple Java program to demonstrate the concept. 
  • The program takes an array of numbers as input and prints the minimum and maximum numbers. 
  • The program is very easy to understand and can be used as a starting point for learning how to find the minimum and maximum number in Java.


Post a Comment

0 Comments