Error on Beginner Code (Tax Calculation)

2 vues (au cours des 30 derniers jours)
Julia O'Bryant
Julia O'Bryant le 16 Déc 2019
Commenté : Guillaume le 16 Déc 2019
Hi! I am brand new to matlab and need some help with this code. I am trying to create a code that asks for the user's net income, calculates the tax, and displays the amount. There is no tax on the first $15,000 of the net income, 5% on every dollar between $15,001-25,000, and 10% tax on every dollar above $25,000. I know that using t=tax is causing the error, but I don't know how else to write the code. Any ideas on how to fix this?
Here is what I have so far:
x = input(Enter your net income: );
t = tax
if x<=15000
t = 0
elseif x>15000 && x<=25000
t = (x-15000) * 0.05
elseif x>25000
t = (x-25000) * 0.1
end
disp(Your tax isnum2str(t))
  3 commentaires
Julia O'Bryant
Julia O'Bryant le 16 Déc 2019
I am just confused because when I do not define t before my if statements, matlab says that t is an undefined variable. I do not know what to define it as or how to write the program so that t is not needed.
Guillaume
Guillaume le 16 Déc 2019
You'll have to explain better the problem, since when I run this code (the same as your minus the t = tax line) I get no error (for valid values of x):
x = input('Enter your net income: ');
if x<=15000
t = 0
elseif x>15000 && x<=25000
t = (x-15000) * 0.05
elseif x>25000
t = (x-25000) * 0.1
end
You will indeed get an undefined t if none of your if tests are true (which may be the case if x is non-scalar or x is NaN

Connectez-vous pour commenter.

Réponse acceptée

Karthick S
Karthick S le 16 Déc 2019
x = input('Enter your net income:');
if x<=15000
t = 0;
elseif x>15000 && x<=25000
t = (x-15000) * 0.05;
elseif x>25000
t = ((x-15000) * 0.05)+((x-25000) * 0.1);
end
disp(sprintf('Your tax is %s', num2str(t)));

Plus de réponses (0)

Catégories

En savoir plus sur Variables dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by