Create a program that prompts the user for 3 numbers and ret

Create a program that prompts the user for 3 numbers and returns the average to the screen.

Solution

Find the solution below:

========================================================================

import java.util.Scanner;

public class Average {
   Scanner sc = new Scanner(System.in);   //creating the Scanner class object

   public static void main(String args[]) {
       Average av = new Average();   //creating class object
       int arr[] = new int[3];       //creating array for three elements
       av.readInput(arr);           //calling the readInput()
       av.findAverage(arr);       //calling the findAverage()
   }

   void readInput(int arr[]) {      
       System.out.println(\"enter the three numbers you want to check average\");
       for (int i = 0; i < 3; i++) {
           arr[i] = sc.nextInt();   //inserting three elements into array
       }
   }

   void findAverage(int arr[]) {
       double average = 0;
       for (int i = 0; i < 3; i++) {
           average += arr[i];           //finding total sum of elements
       }
       average = (average / arr.length); //calculating average of elements
       System.out.println(\"Average of \"+arr.length+\" elements is: \"+average);
   }
}

========================================================================

INPUT:

enter the three numbers you want to check average
5
2
7

OUTPUT:

Average of 3 elements is: 4.666666666666667

=======================================================================

Create a program that prompts the user for 3 numbers and returns the average to the screen.SolutionFind the solution below: ====================================

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site