Write a program that takes as input five numbers and outputs

Write a program that takes as input five numbers and outputs the mean (average) and standard deviation of the numbers. If the numbers are x1,x2,x3,x4, and x5, then the mean is x = (x1+x2+x3+x4+x5)/5 and the standard deviation is:

Please let N =5 be the termination

The program must contain at least the following functions: a function that calculates and returns the mean and a function that calculates the standard deviation.

Solution

//Tested on Eclipse

import java.util.Scanner;

public class MeanDeviation {

   /*
   * Calculation for mean total sum of number/ total number
   */
   public double mean(int number[]) {
       /* Variable declarations */
       double sum = 0, mean;
       /* For loop for calculating sum */
       for (int i = 0; i < number.length; i++) {
           sum += number[i];
       }
       /* Calculating mean */
       mean = sum / number.length;

       return mean;
   }

   /* Standard Deviation method */
   public double standardDeviation(int number[], double mean) {
       /* Variable declaration */
       double sum = 0;
       /* For loop for calculating sum of squares of number-mean */
       for (int i = 0; i < number.length; i++) {

           double num = number[i] - mean;
           sum += (num * num);

       }
       /* Calculating deviation */
       double deviation = Math.sqrt(sum / number.length);

       return deviation;
   }

   public static void main(String[] args) {
       /* Scanner class for input */
       Scanner input = new Scanner(System.in);
       /* Class object creation */
       MeanDeviation meanDeviation = new MeanDeviation();
       System.out.println(\"Please Enter the total number\");
       int N = input.nextInt();
       /* Creating array of integer */
       int number[] = new int[N];
       /* Taking numbers from user */
       for (int i = 0; i < N; i++) {
           System.out.println(\"Please Enter \" + (i + 1) + \" Number\");
           number[i] = input.nextInt();
       }
       /* calling mean method for mean calculation */
       double mean = meanDeviation.mean(number);
       System.out.println(\"Mean of Given Number is \" + mean);

       double deviation = meanDeviation.standardDeviation(number, mean);
       System.out.println(\"Standard Deviation of Given Number is \" + String.format(\"%.2f\", (double) deviation));
   }

}


/**********output***********/

Please Enter the total number
5
Please Enter 1 Number
1
Please Enter 2 Number
2
Please Enter 3 Number
3
Please Enter 4 Number
4
Please Enter 5 Number
5
Mean of Given Number is 3.0
Standard Deviation of Given Number is 1.41

Thanks a lot

Write a program that takes as input five numbers and outputs the mean (average) and standard deviation of the numbers. If the numbers are x1,x2,x3,x4, and x5, t
Write a program that takes as input five numbers and outputs the mean (average) and standard deviation of the numbers. If the numbers are x1,x2,x3,x4, and x5, t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site