QuestionAsk Questions?

Write a program that inputs a series of integers into an array. The numbers are in the range 0 to 100. Calculate and display : a) The Largest number b) The Smallest Number c)The sum of the numbers d)The mean of the numbers

View Answers

March 2, 2011 at 2:52 PM

**Java Array Example**

import java.util.*; class ArrayExample6{ public static int getMaxValue(int[] arr){ int maxValue = arr[0]; for(int i=1;i < arr.length;i++){ if(arr[i] > maxValue){ maxValue = arr[i]; } } return maxValue; } public static int getMinValue(int[] arr){ int minValue = arr[0]; for(int i=1;i<arr.length;i++){ if(arr[i] < minValue){ minValue = arr[i]; } } return minValue; } public static void main(String[] args) { int sum=0; Scanner input=new Scanner(System.in); System.out.println("Enter Array Elements: "); int array[]=new int[10]; for(int i=0;i<array.length;i++){ int num=input.nextInt(); if(num<0||num>100){ System.out.print("Not in a range(0-100)!Re-enter number: "); num=input.nextInt(); } array[i]=num; sum+=array[i]; } System.out.println("Sum Of Array Elements is: "+sum); System.out.println("Mean of Array Elements is: "+sum/array.length); System.out.println("Smallest No: "+getMinValue(array)); System.out.println("Largest No: "+getMaxValue(array)); } }

Related Tutorials/Questions & Answers: