Java a The mean of a list of numbers is its arithmetic avera

Java:

a. The mean of a list of numbers is its arithmetic average. The median of a list is its middle value when the values are placed in order. For example, if a list contains 1, 4, 7, 8, and 10, then the mean is 6 and the median is 7. Write an application that allows you to enter five integers and displays the values, their mean, and their median.

Save the file as MeanMedian.java.

b. Revise the MeanMedian class so that the user can enter any number of values up to 20. If the list has an even number of values, the median is the numeric average of the values in the two middle positions. Save the file as

MeanMedian2.java

Solution


Ans a):
import java.util.*;
import java.lang.*;
import java.io.*;


class MeanMedian
{
   public static void main (String[] args) throws java.lang.Exception
   {
   Scanner scan=new Scanner(System.in);
   int arr[]=new int[5];
   System.out.println(\"Enter the integers :\"); //asking the user to enter 5 integers
   for(int i=0;i<5;i++)
   arr[i]=scan.nextInt();
   int sum=0;
   for(int i=0;i<5;i++)
   sum=sum+arr[i];
   int mean=sum/5; //calculating mean
   int median=arr[2]; //calculating median
   System.out.println(\"Integers entered by the user \");
   for(int i=0;i<5;i++)
   System.out.print(arr[i]+\" \"); //printing values entered by user
   System.out.println();
   System.out.println(\"Mean = \"+mean); //printing mean
   System.out.println(\"Median = \"+median); //printing median
      
   }
}

******OUTPUT******
Enter the integers :
1 4 7 8 10
Integers entered by the user
1 4 7 8 10
Mean = 6
Median = 7
******OUTPUT******

Ansb):


import java.util.*;
import java.lang.*;
import java.io.*;


class MeanMedian //class MeanMedian
{
   public static void main (String[] args) throws java.lang.Exception
   {
   int sum=0; //variable to store the sum of values
   int mean=0; //variable to store the mean of values
   int median=0; //variable to store the median of values
   Scanner scan=new Scanner(System.in);
  
   System.out.println(\"Enter the number of values upto you want to enter,it must not be greater than 20 :\"); //asking the user for no.of values
   int n=scan.nextInt(); //store size in n
   int arr[]=new int[n]; //array to store integers
       System.out.println(\"Enter the integers\"); //asking the user to enter the integers
   for(int i=0;i<n;i++)
   arr[i]=scan.nextInt(); //storing integers in array
     
   for(int i=0;i<n;i++)
   sum=sum+arr[i];
   mean=(sum/n); //calculating mean
   if(n%2==0) //if even no.of integers enteres
   {int index1=n/2;
   int index2=index1-1;
   median=((arr[index1]+arr[index2])/2); //calculating median
  
   }
   else //odd no.of integers
   {int index1=n/2;
   median=(arr[index1]); //calculating median
   }
   System.out.println(\"Integers entered by the user \");
   for(int i=0;i<n;i++)
   System.out.print(arr[i]+\" \"); //printing values entered by user
   System.out.println();
   System.out.println(\"Mean = \"+mean); //printing mean
   System.out.println(\"Median = \"+median); //printing median
      
   }
}

********OUTPUT*********
Enter the number of values upto you want to enter,it must not be greater than 20 :
10
Enter the integers
1 4 7 10 13 16 19 22 25 28
Integers entered by the user
1 4 7 10 13 16 19 22 25 28
Mean = 14
Median = 14
********OUTPUT*********

Note:In case of any doubt please ask the question,thanks.

Java: a. The mean of a list of numbers is its arithmetic average. The median of a list is its middle value when the values are placed in order. For example, if
Java: a. The mean of a list of numbers is its arithmetic average. The median of a list is its middle value when the values are placed in order. For example, if

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site