Java Software Solutions > PP2.2 > Solution

Problem:

Write an application that reads three integers and prints their
average.

Output:

Enter the first number: 5
Enter the second number: 6
Enter the third number: 6
The average is: 5.666666666666667

Solution:

import java.util.Scanner;

class AverageOfThree
{
   //-----------------------------------------------------------------
   //  Reads three integers from the user and prints their average.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      int num1, num2, num3;
      double average;
   Scanner scan = new Scanner (System.in);

      System.out.print ("Enter the first number: ");
      num1 = scan.nextInt();
      System.out.print ("Enter the second number: ");
      num2 = scan.nextInt();
      System.out.print ("Enter the third number: ");
      num3 = scan.nextInt();

      average = (double) (num1+num2+num3) / 3;

      System.out.println ("The average is: " + average);
   }
}
-----------------------------------------------------
   //  Prints a quote from Abraham Lincoln, including quotation
   //  marks.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      System.out.println ("A quote by Abraham Lincoln:");

      System.out.println ("\"Whatever you are, be a good one.\"");
   }
}

No comments:

Post a Comment