Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Calculate adjusted salary and tax with deductions: Using loops. A program may ex

ID: 3915301 • Letter: C

Question

Calculate adjusted salary and tax with deductions: Using loops.

A program may execute the same computations repeatedly.

The program below repeatedly asks the user to enter an annual salary, stopping when the user enters 0 or less. For each annual salary, the program determines the tax rate and computes the tax to pay.

Run the program below with annual salaries of 40000, 90000, and then 0.

Modify the program to use a while loop inside the given while loop. The new inner loop should repeatedly ask the user to enter a salary deduction, stopping when the user enters a 0 or less. The deductions are summed and then subtracted from the annual income, giving an adjusted gross income. The tax rate is then calculated from the adjusted gross income.

Run the program with the following input: 40000, 7000, 2000, 0, and 0. Note that the 7000 and 2000 are deductions.

Explanation / Answer


annual salaries of 40000, 90000, and then 0.

Output:


Enter annual salary (0 to exit) :40000
Annual salary : 40000
Tax Rate : 0.2
Tax to pay : 8000

Enter annual salary (0 to exit) :90000
Annual salary : 90000
Tax Rate : 0.3
Tax to pay : 27000

Enter annual salary (0 to exit) :0

Code for Deductions:

import java.util.Scanner;


class IncomeTax
{
public static void main (String[] args)
{
  Scanner scnr = new Scanner(System.in);
  
  final String SALARY_PROMPT = " Enter annual salary (0 to exit) : ";
  int annualSalary;
  int deduction;
  int totalDeductions;
  double taxRate;
  int taxToPay;
  int sumDeductions;
  
  System.out.println(SALARY_PROMPT);
  annualSalary = scnr.nextInt();
  
  while(annualSalary > 0)
  {
   
  if(annualSalary <= 20000)
  taxRate = 0.10;
  else if(annualSalary <= 50000)
  taxRate = 0.20;
  else if(annualSalary <= 100000)
  taxRate = 0.30;
  else
  taxRate = 0.40;
  
  System.out.println("Enter the salary deduction (0 to exit) : ");
  deduction = scnr.nextInt();
  sumDeductions = 0;
  while(deduction > 0)
  {
   sumDeductions = sumDeductions + deduction;// add all deductions
   System.out.println("Enter the salary deduction : ");
   deduction = scnr.nextInt();
  }
  
  annualSalary = annualSalary - sumDeductions; // adjust annualSalary for deductions
  taxToPay = (int)(annualSalary * taxRate);
  
  
  System.out.println("Annual salary : "+annualSalary);
  System.out.println("Tax Rate : "+taxRate);
  System.out.println("Tax to pay : "+taxToPay);
  
  
  System.out.println(SALARY_PROMPT);
  annualSalary = scnr.nextInt();
  
  }
  
}
}

Output:

Enter annual salary (0 to exit) :40000
Enter the salary deduction (0 to exit) :7000
Enter the salary deduction :2000
Enter the salary deduction :0
Annual salary : 31000
Tax Rate : 0.2
Tax to pay : 6200

Enter annual salary (0 to exit) :70000
Enter the salary deduction (0 to exit) :1000
Enter the salary deduction :3000
Enter the salary deduction :0
Annual salary : 66000
Tax Rate : 0.3
Tax to pay : 19800

Enter annual salary (0 to exit) : 0

Do ask if any doubt. Please upvote.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote