Question

In: Computer Science

Question: Rewrite the program in C which computes the tax due based on a tax table....

Question: Rewrite the program in C which computes the tax due based on a tax table. Instead of using the if/else structure, use the switch statement. /* Pre : salary is predefined *Post: Returns the tax due for 0.0 <= salary <= 150,000.00; returns -1.0 if salary is outside of table range. */

double comp_tax(double salary)

{

double tax;

if (salary < 0.0)

tax = -1.0;

else if (salary < 15000.00)

tax = 0.15 * salary;

else if (salary < 30000.00)

tax = (salary - 15000.00) * 0.18 + 2250.00 ;

else if (salary < 50000.00)

tax = (salary - 30000.00) * 0.22 + 5400.00;

else if (salary < 80000.00)

tax = (salary - 50000.00) * 0.27 + 11000.00;

else if (salary < 150000.00)

tax = (salary - 80000.00) * 0.33 +21600.00;

else

tax = -1.0; return (tax);

}

Solutions

Expert Solution

Used switch statement for each condition exists to calculate tax due based on given table.

(Note that we cannot use single switch statement to calculate result for all conditions , as cases in switch statement needed to be of exact value we cannot use comparison for each cases)

That's why used switch statement for each condition to calculate tax and use condition to calculate tax as expression for switch statement if this expression is true then it will go to case:true else it will hit default case which is empty means controll gets out of switch statement and check result from other switch statement.

double comp_tax(double salary)

{

double tax;

    //when salary is out of range we make value of tax = -1
    switch (salary < 0.0 || salary > 150000.00) //condition for switch statement
    {
    case true: //if condition is true then tax becomes -1
        tax = -1.0;
        break;
    default:
        break;
    }
    //when salary is less then 15000.00
    switch (salary < 15000.00 && salary >= 0.0)
    {
    case true:
        tax = 0.15 * salary;
        break;
    default:
        break;
    }
    //when salary is less then 30000.00
    switch (salary < 30000.00 && salary >= 15000.00)
    {
    case true:
        tax = (salary - 15000.00) * 0.18 + 2250.00;
        break;
    default:
        break;
    }
    //when salary is less then 50000.00
    switch (salary < 50000.00 && salary >= 30000.00)
    {
    case true:
        tax = (salary - 30000.00) * 0.22 + 5400.00;
        break;
    default:
        break;
    }
    //when salary is less then 80000.00
    switch (salary < 80000.00 && salary >= 50000.00)
    {
    case true:
        tax = (salary - 50000.00) * 0.27 + 11000.00;
        break;
    default:
        break;
    }
    //when salary is less then 150000.00
    switch (salary <= 150000.00 && salary >= 80000.00)
    {
    case true:
        tax = (salary - 80000.00) * 0.33 + 21600.00;
        break;
    default:
        break;
    }
    return (tax);

}

OUTPUT :


Related Solutions

C++ program Overloaded Hospital Write a c++ program that computes and displays the charges for a...
C++ program Overloaded Hospital Write a c++ program that computes and displays the charges for a patient’s hospital stay. First, the program should ask if the patient was admitted as an inpatient or an outpatient. If the patient was an inpatient, the following data should be entered: The number of days spent in the hospital The daily rate Hospital medication charges Charges for hospital services (lab tests, etc.) The program should ask for the following data if the patient was...
Write a Python program that computes the income tax for an individual. The program should ask...
Write a Python program that computes the income tax for an individual. The program should ask the user to enter the total taxable income for the year. The program then uses the tax bracket (as shown below) to calculate the tax amount. This is based on a progressive income tax system which is how income tax is calculated in the U.S. As a result, this for example means that only income above $500,001 is taxed at 37%. Income of lower...
Write a Python program that computes the income tax for an individual. The program should ask...
Write a Python program that computes the income tax for an individual. The program should ask the user to enter the total taxable income for the year. The program then uses the tax bracket (as shown below) to calculate the tax amount. This is based on a progressive income tax system which is how income tax is calculated in the U.S. As a result, this for example means that only income above $500,001 is taxed at 37%. Income of lower...
Write a program that computes the tax and tip on a restaurant bill for a patron...
Write a program that computes the tax and tip on a restaurant bill for a patron with $44.50 meal charge. The tax should be 6.75% of the meal cost. The tip should be 15% of the total after adding tax. Display the meal cost, tax amount, tip amount, and total bill on the screen. (I need this to be in OOP using C++)
Write a program that computes the tax and tip on a restaurant bill for a patron...
Write a program that computes the tax and tip on a restaurant bill for a patron with $44.50 meal charge. The tax should be 6.75% of the meal cost. The tip should be 15% of the total after adding tax. Display the meal cost, tax amount, tip amount, and total bill on the screen. (I need this to be in OOP).
Write a defining table and a computer program that computes and outputs the volume of a...
Write a defining table and a computer program that computes and outputs the volume of a torus with inner radius a and outer radius b. A doughnut is an example of a torus. Your program must read the inner radius and outer radius from two text fields and display the volume in a div. The formula for the volume of a torus is v =  π2(a + b)(a - b)2 4 where v is the volume, π is the constant pi,...
Write a defining table and a JavaScript program that computes and outputs the rate of change...
Write a defining table and a JavaScript program that computes and outputs the rate of change of a price. The program must allow the user to enter a previous price and a current price. The formula for the rate of change is rateOfChange =  currPrice - prevPrice prevPrice Your program should allow a user to enter real numbers such as 7.54. If you wish, you may use the following HTML code in your program. <!DOCTYPE HTML> <html lang="en-us"> <head> <meta charset="utf-8">...
Write a program in C that computes the area of a circle (Area = pi *...
Write a program in C that computes the area of a circle (Area = pi * r2) and the volume of a sphere (Volume = 4/3 * pi * r3). Both formulas use r which is the radius. Declare a float variable pi = 3.14159. Get the value of r from the keyboard and store it in a float variable. Display both the area of the circle and the volume of the sphere.
**C++ Programming Language** a program that computes the average of a collection of numbers and then...
**C++ Programming Language** a program that computes the average of a collection of numbers and then outputs the total number of values that are greater than the average. The objective of this assignment is to select a language that you have never used before and modify the program from the text so that it outputs the number of A's, B's, C's, D's and F's. An A grade is any score that is at least 20% greater than the average. The...
Write a program that determines the income tax rates for a state. Tax rates are based on the salary according to the following table:
in java  Code Problem 2 Write a program that determines the income tax rates for a state.  Tax rates are based on the salary according to the following table: 0 – 25000 Dollars                    10% 25001 - 50000 Dollars            15% 50001 - 75000 Dollars             20% Over 75000 Dollars                 35% Write the program so that it asks the user how many taxpayers should be processed and use the number of taxpayers to control the end of the program. For each taxpayer, you will need to input the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT