Skip to main content
removed fluff
Source Link
Mark Rotteveel
  • 110.3k
  • 240
  • 160
  • 233

I am trying to get the sum, average, max and min value from user input in array. sum, average and max is giving the correct output. But min value is not working. Where am I doing wrong would someone help me please to find out this. And please don't be bother for this basic question.?

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); } } 

Output:

Enter elements you want to input in array: 5 Enter all the elements: 25 5 10 6 4 Sum is: 50 Average is: 10.0 Max is: 25 Min is: 0 

Min value should be 4.

I am trying to get the sum, average, max and min value from user input in array. sum, average and max is giving the correct output. But min value is not working. Where am I doing wrong would someone help me please to find out this. And please don't be bother for this basic question.

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); } } 

Output:

Enter elements you want to input in array: 5 Enter all the elements: 25 5 10 6 4 Sum is: 50 Average is: 10.0 Max is: 25 Min is: 0 

Min value should be 4.

I am trying to get the sum, average, max and min value from user input in array. sum, average and max is giving the correct output. But min value is not working. Where am I doing wrong would someone help me please to find out?

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); } } 

Output:

Enter elements you want to input in array: 5 Enter all the elements: 25 5 10 6 4 Sum is: 50 Average is: 10.0 Max is: 25 Min is: 0 

Min value should be 4.

fixed title to make it shorter and more informative
Link

get sum, average, min, max Finding smallest value from user input array in java

Source Link
Rashed Hasan
  • 3.8k
  • 11
  • 43
  • 91

get sum, average, min, max from user input array in java

I am trying to get the sum, average, max and min value from user input in array. sum, average and max is giving the correct output. But min value is not working. Where am I doing wrong would someone help me please to find out this. And please don't be bother for this basic question.

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); } } 

Output:

Enter elements you want to input in array: 5 Enter all the elements: 25 5 10 6 4 Sum is: 50 Average is: 10.0 Max is: 25 Min is: 0 

Min value should be 4.