Thursday, June 19, 2014

Elementary Programming; Exercise No. 5

Write a program that reads the subtotal and the gratuity rate, then computes the gratuity and total. For example, if the user enters  10 for subtotal and  15% for gratuity rate, the program displays  $1.5
as gratuity and  $11.5 as total.

Solution:

public static void main(String[] args) {
   Scanner s = new Scanner(System.in);
   double subtotal, gratuityRate, total, gratuity;

   System.out.print("Enter a value for subtotal:");
   subtotal = s.nextDouble();

   System.out.print("Enter a value for gratuity rate:");
   gratuityRate = s.nextDouble();

   gratuity = (subtotal * gratuityRate) / 100;
   total = subtotal + gratuity;

   System.out.print("The gratuity is $" + gratuity + " and total is $" + total + ".");
}

1 comment:

Author