Question: Rewrite the program in C language, which computes the tax due based on a tax table. Instead of using the if/else structure, use the switch statement.
/*Computes the tax due based on a tax table.
*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);
}
**This is from the book "Problem Solving and Program Design in C 8th edition on page 212 table 4.11**
ANSWER :
CODE :
double comp_tax(int salary)
{
double tax;
switch ( salary < 0 || salary>150000) //condition 1
{
case 1 :
tax = -1.0;
break;
}
switch(salary>=0 && salary<15000) //conditon 2
{
case 1 :
tax = 0.15*salary;
break;
}
switch(salary>=15000 && salary<30000) //condition
3
{
case 1 :
tax = (salary-15000.00)*0.18 + 2250.00;
break;
}
switch(salary>=30000 && salary<50000) //condition
4
{
case 1 :
tax = (salary-30000.00)*0.22 + 5400.00;
break;
}
switch(salary>=50000 && salary<80000) //condition
5
{
case 1 :
tax = (salary-50000.00)*0.27 + 11000.00;
break;
}
switch(salary>=80000 && salary<150000) //condition
6
{
case 1 :
tax = (salary-80000.00)*0.33 + 21600.00;
break;
}
return tax;
}
Get Answers For Free
Most questions answered within 1 hours.