Error on Beginner Code (Tax Calculation)
8 views (last 30 days)
Show older comments
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 is’ num2str(t))
3 Comments
Guillaume
on 16 Dec 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
Accepted Answer
Karthick S
on 16 Dec 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)));
0 Comments
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!