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 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...
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...
Using C Language Write a program segment that computes 1 + 2 + 3 + ......
Using C Language Write a program segment that computes 1 + 2 + 3 + ... + ( n - 1) + n , where n is a data value. Follow the loop body with an if statement that compares this value to (n * (n + 1)) / 2 and displays a message that indicates whether the values are the same or different. Please give me code to just copy and paste
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT