[Subject: C Programming] 1. Modify the program below so that it uses a function
ID: 3760992 • Letter: #
Question
[Subject: C Programming]
1. Modify the program below so that it uses a function to compare the amount of income tax. When passed an amount of taxable income, the function will return the tax due.
#include
int main(void) {
float commission, value;
printf("Enter value of trade: ");
scanf("%f", &value);
if(value < 2500.00f)
commission = 30.00f+.017f * value;
elseif(value < 6250.00f)
commission = 56.00f + .0066f * value;
elseif(value < 20000.00f)
commission = 76.00f + .0034f * value;
elseif(value < 50000.00f)
commission = 100.00f + .0022f * value;
elseif(value < 500000.00f)
commission = 155.00f + .0011f * value;
else
commission = 255.00f + .0009f * value;
if(commission < 39.00f)
commission = 39.00f;
printf("Commission: $.2f ", commission);
return 0; }
2. The power function can be made faster by having it calculate x^n in a different way. We first notice that if n is a power of 2, then x^n can be computed by squaring. For example, x^4 is a square of x^2, so x^4 can be computed using only two multiplication instead of three. As it happens, this technique can be used even when n is not a power of 2. If n is even, we use the formula x^n = (x^n/2)^2. If n is odd, then x^n = x times x^n-1. Write a recursive function that computes x^n.(The recursion ends when n=0, in which case the function returns 1.) To test your function, write a program that asks the user to enter values for x and n, calls power to compute x^n, and then displays the value returned by the function.
Explanation / Answer
#include <stdio.h>
int main() {
double income, tax;
double commission, value;
/* get the income from the user */
printf("Enter your income:");
scanf("%lf", &value);
/* calculate the income tax */
if(value < 2500)
{
commission = 30+.017 * value;
}
elseif(value < 6250)
{
commission = 56 + .0066* value;
}
elseif(value < 20000)
{
commission = 76 + .0034 * value;
}
elseif(value < 50000)
{
commission = 100+ .0022 * value;
}
elseif(value < 500000)
{
commission = 155 + .0011 * value;
}
else
{
commission = 255+ .0009 * value;
}
if(commission < 39)
{
commission = 39;
}
/* print the result */
printf("Income tax for %.2lf is %.2lf ", value, commission);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.