Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is my code to get the sum, average, max and min value from user as Array:

import java.util.Scanner;

public class minMaxSumAverage {

    public static void main(String args[]) {

        int n, sum = 0, max, min;

        double average = 0;

        Scanner s = new Scanner(System.in);

        System.out.println("Enter elements you want to input in array: ");

        n = s.nextInt();

        int a[] = new int[n];

        max = a[0];

        min = a[0];

        System.out.println("Enter all the elements:");

        for (int i = 0; i < n; i++) {

            a[i] = s.nextInt();

            sum += a[i];

            average = (double) sum/a.length;

            if (a[i] > max) {

                max = a[i];

            }

            if (a[i] < min) {

                min = a[i];

            }

        }

        System.out.println("Sum is: " + sum);

        System.out.println("Average is: " + average);

        System.out.println("Max is: " + max);

        System.out.println("Min is: " + min);

    }

}

Here’s my output: 

Enter elements you want to input in the array: 5

Enter all the elements: 25, 5, 10, 6, 4

The sum is: 50

Average is: 10.0

Max is: 25

Min is: 0

Min value should be 4.

Can anyone tell me why the min value is not working in my code? 

1 Answer

0 votes
by (19.7k points)

Below is the updated version of your code to get the min value:

Scanner scan = null;

        try {

            int n, sum = 0, max, min;

            double average = 0;

            scan = new Scanner(System.in);

            System.out.println("Enter elements you want to input in array: ");

            n = scan.nextInt();

            int a[] = new int[n];

            max = a[0];

            System.out.println("Enter all the elements:");

            for (int i = 0; i < n; i++) {

                a[i] = scan.nextInt();

                sum += a[i];

                average = (double) sum/a.length;

                if (a[i] > max) {

                    max = a[i];

                }

                /**

                // from here remove logic for get min value.

                if (a[i] < min) {

                   min = a[i];

                }

                **/

            }

            min = a[0];

            for(int i=0;i<a.length;i++){

                if(a[i] < min){

                  min = a[i];

                }

              }

            System.out.println("Sum is: " + sum);

            System.out.println("Average is: " + average);

            System.out.println("Max is: " + max);

            System.out.println("Min is: " + min);

        }

        catch (Exception ex) {

            ex.printStackTrace();

        }finally{

            scan.close();

        }

The output for the above code: 

Enter elements you want to input in the array: 5

Enter all the elements: 25, 5, 10, 6, 4

Output :

The sum is: 50

Average is: 10.0

Max is: 25

Min is: 4


 

Interested in Java? Check out this Java tutorial by Intellipaat. 

Related questions

0 votes
1 answer
asked Apr 16, 2021 in Java by sheela_singh (9.5k points)
0 votes
1 answer
0 votes
4 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...