1 Replies - 747 Views - Last Post: 02 November 2012 - 04:25 AM Rate Topic: -----

#1 ag7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-November 12

Answer should be a decimal, not rounded to whole number in while loop

Posted 01 November 2012 - 09:43 PM

I am trying to see how many years it will take for me to save a million dollars using the following conditions: monthlyWageAfterTaxes = 2700; numberOfYears=30;
Expenses.... rent=800; utilities=300; insurance=200; auto=250; food_misc=325; My rent increases $200 every five years,first time in month 61 and my monthlyWageAfterTaxes increases 2% every year,first time in month 13.

Here is my code so far:
% input statements
monthlyWageAfterTaxes = 2700;
savingsGoal= input('Type in a savings goal: ');

%Expenses
rent=800;
utilities=300;
insurance=200;
auto=250;
food_misc=325;
totalSavings=0;
numYears=0;

while totalSavings < savingsGoal
monthlyBills= rent+utilities+insurance+auto+food_misc;

if rem(numYears,5) ==0;
rent = rent + 200;
end
totalSavings = totalSavings + monthlyWageAfterTaxes*12 - monthlyBills*12
monthlyWageAfterTaxes = monthlyWageAfterTaxes * 1.02;
numYears= (numYears+1)


end

My problem is that I want to show the number of years as a decimal instead of the code rounding to the nearest whole year. I worked it out on paper and I get 48.16667 years but when I run the code, the answer is 51 years. Can anyone see what my error is? I am trying to solve this problem by just using years, but do I need to solve using months and then divide by 12 at the end of the code?

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Answer should be a decimal, not rounded to whole number in while loop

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,038
  • Joined: 21-June 11

Re: Answer should be a decimal, not rounded to whole number in while loop

Posted 02 November 2012 - 04:25 AM

When asking a question in the Other Languages forum, you should mention which language you're talking about - preferably in the title.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1