Write a Java program as follows in the main method prompt th


Write a Java program as follows: in the main method, prompt the user for an integer number. Store the number in a variable called input Num. Pass the input Num to a method called Avg. In compute Avg, prompt the user to input double values as many times as inputNum. computeAvg method should calculate and return the average of real that user entered. Print the result in your main method.

Solution

/**

*See the comments for explanation

*/

import java.util.Scanner;

public class computeAverage {

   /**
   * @param args

*Main method
   */
   public static void main(String[] args) {
       int inputNum = 0;
       // Scanner instance to take the input from system console
       Scanner sc = new Scanner(System.in);
       System.out.println(\"How many number you want to enter\");
       inputNum = sc.nextInt();
       System.out.println(\"Average of numbers entered is \" + computeAverage(inputNum));

   }

   /**
   *
   * @param inputNum
   * @return average This method takes the number of numbers to be entered and
   * returns the average of the entered numbers
   */

   private static double computeAverage(int inputNum) {
       int iterator = 0;
       double sum = 0, average = 0;
       // Scanner instance to take the input from system console
       Scanner sc = new Scanner(System.in);
       // taking the input and calculating the sum
       for (; iterator < inputNum; iterator++) {
           System.out.println(\"Enter non fractional number\");
           sum = sum + sc.nextDouble();
       }
       // average of numbers =sum of numbers /no of numbers
       average = sum / inputNum;
       return average;
   }

}

 Write a Java program as follows: in the main method, prompt the user for an integer number. Store the number in a variable called input Num. Pass the input Num

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site