I'm trying to write a for loop but one of the values won't update. R

I'm just trying to write a simple for loops so I can see how much the person would owe after each consecutive year. It ends up printing the statement with only the n value ever changed. The A stays the same as the first time.

for (n in 1:15){
  A <-  5000 * (1+ .115/100) ^n
  sprintf("%.2f owed after %.f years", A, n) 
 }

I have no clue what to do to fix it. Thanks

1 answer

  • answered 2018-01-11 20:50 satan

    You're missing a print statement-

       for (n in 1:15){
       A <-  5000 * (1+ .115/100) ^n
       print(sprintf("%.2f owed after %.f years", A, n)) 
       }