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

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact