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

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 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.
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">...
In the space provided below write a C ++ program that computes the total amount of...
In the space provided below write a C ++ program that computes the total amount of money you are depositing in your bank account. Your program does this by asking you to first enter the number and amount of each check you are depositing, and then it asks you to enter the type of cash bills being deposited and how many of each type, also the types of coins being deposited, and the number of each coin type.
Write a C++ program that inputs a sequence of integers into a vector, computes the average,...
Write a C++ program that inputs a sequence of integers into a vector, computes the average, and then outputs the # of input values, the average, and the values themselves. There are 0 or more inputs values, followed by a negative value as the sentinel; the negative value is not stored and not counted. The following sample input: 10 20 0 99 -1 would produce the following output: N: 4 Avg: 32.25 10 20 0 99 The main program has...
Write a C++ program that inputs a sequence of integers into a vector, computes the average,...
Write a C++ program that inputs a sequence of integers into a vector, computes the average, and then outputs the # of input values, the average, and the values themselves. There are 0 or more inputs values, followed by a negative value as the sentinel; the negative value is not stored and not counted. The following sample input: 10 20 0 99 -1 would produce the following output: N: 4 Avg: 32.25 10 20 0 99 The main program has...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT