a code that compute numbers and numbers does not !

2 views (last 30 days)
basim almutairi
basim almutairi on 15 Sep 2021
Answered: Sreedhar Arumugam on 15 Sep 2021
greetings all,
the following code I've created to compute an amount. whenever I try number of years let's say 2 it will compute correctly. However, when I want to compute something like 10 years it wont work ! please can you adivse
p = input('enter the amount you want to deposit initially:');
n = input('enter number of years:');
t = input(['enter the frequency of interest compounding\n'...
'type 1: for annually\n'...
'type 2: for semi annually\n'...
'type 4: for quarterly\n'...
'type 12: for monthly\n']);
r = input('enter the interest rate:');
switch n
case (1)
a = p*((1+((0.01*r)/t))^(n*t));
disp(['annually:',num2str(a)]);
case (2)
a = p*((1+((0.01*r)/t))^(n*t));
disp(['semi annually:',num2str(a)]);
case (4)
a = p*((1+((0.01*r)/t))^(n*t));
disp(['quarterly:',num2str(a)]);
case (12)
a = p*((1+((0.01*r)/t))^(n*t));
disp(['monthly:',num2str(a)]);
end

Answers (1)

Sreedhar Arumugam
Sreedhar Arumugam on 15 Sep 2021
I am assuming you want to compute interest based on the frequency of compounding.
Your switch statement is with respect to n (number of years) when it should actually be with respect to t (frequency of compounding).
switch t
case(1)
case (2)
case (4)
case(12)
end
This is why it fails for n=10 in your code as there is no case corresponding for this input.

Community Treasure Hunt

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

Start Hunting!

Translated by