-3

Trying to find the future value of a single amount.

I have created and defined the present value, rate and years. They all work together and now I am trying to figure out how to take the numbers entered for each and calculate them for the final value.

The formula I am using is:

(fv = pv * (1+r) ** n)

but am unsure how to get Ruby to run the calculation?

Here is my program code:

#CALCULATION OF FUTURE VALUE FOR A SINGLE AMOUNT

def input 

    count = 0
    puts "How much money you would like to invest?  The amount must be at least $100 but no more than $50,000.  (ex. 1000 = $1,000)"
    pv = gets.chomp.to_i

        while ((count < 3) && (pv < 100 || pv > 50000))
            puts""
            puts "YOU MUST ENTER A VALID NUMBER.  PLEASE TRY AGAIN."
            puts ""
            count = count + 1
            puts""
            puts "How much money you would like to invest?  The amount must be at least $100 but no more than $50,000.  (ex. 1000 = $1,000)"
            pv = gets.chomp.to_i

            while ((count < 3) && (pv < 100 || pv > 50000))
                puts""
                puts "YOU MUST ENTER A VALID NUMBER.  PLEASE TRY AGAIN."
                count = count + 1
                puts""
                puts "How much money you would like to invest?  The amount must be at least $100 but no more than $50,000.  (ex. 1000 = $1,000)"
                pv = gets.chomp.to_i
            end 

        end

        if (count >= 3)
            puts ""
            puts "TRY AGAIN LATER..."
            puts "" && exit
            return pv
        end

    count = 0
    puts "What is the annual rate of interest? (ex. .08 = 8%)  The rate must be greater than 0.00 and less than or equal to 0.20"
    r = gets.chomp.to_f 

        while ((count <3) && (r < 0.00 || r > 0.20))
            puts""
            puts 'YOU MUST ENTER A VALID RATE.  PLEASE SEE EXAMPLE ABOVE.'
            puts ""
            count = count + 1
            puts ""
            puts "What is the annual rate of interest? (ex. .08 = 8%)  The rate must be greater than 0.00 and less than or equal to 0.20"
            r = gets.chomp.to_f 

            while ((count <3) && (r < 0.00 || r > 0.20))
                puts 'YOU MUST ENTER A VALID RATE.  PLEASE TRY AGAIN.'
                puts ""
                count = count + 1
                puts ""
                puts "What is the annual rate of interest? (ex. .08 = 8%)  The rate must be greater than 0.00 and less than or equal to 0.20"
                r = gets.chomp.to_f 
            end
        end 

        if (count >=3)
            puts ""
            puts "TRY AGAIN LATER..." 
            puts "" && exit
            return r 
        end

    count = 0
    puts "How many years will the investment be for? (ex. 2 = 2 years)  Please enter a number between 1 and 30."
    n = gets.chomp.to_i

        while ((count  < 3) && (n < 1 || n > 30))
            puts""
            puts"YOU MUST ENTER AT LEAST ONE YEAR.  PLEASE SEE EXAMPLE ABOVE."
            puts""
            count = count + 1
            puts ""
            puts "How many years will the investment be for? (ex. 2 = 2 years)  Please enter a number between 1 and 30."
            n = gets.chomp.to_i
            while ((count  < 3) && (n < 1 || n > 30))
                puts""
                puts"PLEASE ENTER A VALID NUMBER.  PLEASE TRY AGAIN."
                puts""
                count = count + 1
                puts""
                puts "How many years will the investment be for? (ex. 2 = 2 years)  Please enter a number between 1 and 30."
                n = gets.chomp.to_i
            end
end

        if (count >= 3)
            puts""
            puts "TRY AGAIN LATER..." 
            puts "" && exit
            return n    
        end
end

puts input


def final_value (fv)

    pv = 1000     #want user's input here 
    r  = 0.05     #want user's input here
    n  = 5        #want user's input here
    fv = pv * (1+r) ** n
    puts "After #{n} years you will have $#{fv.round(2)}!"
    puts ""
    puts "Press ENTER to exit."
end

puts final_value ()

gets
sawa
  • 156,411
  • 36
  • 254
  • 350
Renee
  • 7
  • 2

2 Answers2

0

You can define a method like this:

def final_value (pv, r, n)
    pv * (1+r) ** n
end

And, then call the method with different pv, r, n values. e.g.

# pv = 100
# r = 0.5
# n = 10

# take input from user in console
puts 'Enter pv: '
pv = gets.strip.to_f
puts 'Enter r: '
r = gets.strip.to_f
puts 'Enter n: '
n = gets.strip.to_f

fv = final_value(pv, r, n)
puts "fv: #{fv}"
# => fv: 5766.50390625
K M Rakibul Islam
  • 31,427
  • 11
  • 79
  • 100
  • What if I have the user enter the pv, r and n values? How do I store the entry to use for this method? – Renee Oct 10 '15 at 04:16
  • See my updated answer. You can take input from the user using: `gets` method and then do: `get.strip` to strip the spaces and newlines in the input and then `.to_f` to convert the string to a float value. And, save it in the respective variable as shown in my updated answer. Let me know if you have any other question. – K M Rakibul Islam Oct 10 '15 at 04:26
  • I am getting an error when I def the method... " 'final_value': wrong number of arguments (1 for 3) (ArgumentError) – Renee Oct 13 '15 at 02:00
  • You are passing 1 argument to the `final_value` method but its expecting 3. Call it like this: `final_value(100, 0.5, 10)` – K M Rakibul Islam Oct 13 '15 at 03:02
  • It gives me a syntax error, unexpected tINTEGER, expecting ') def final_value (100, 0.05, 10) – Renee Oct 13 '15 at 03:38
  • If you just copy my answer and paste it to your file, then it will work without any syntax error. – K M Rakibul Islam Oct 13 '15 at 03:58
  • Nope, it still doesn't work. Would it be better if I post my entire code so you can see where I am going wrong? I am a beginner so I have no idea what I am doing. – Renee Oct 13 '15 at 04:06
  • ok, update your question with your latest code and i will take a look. – K M Rakibul Islam Oct 13 '15 at 04:07
0

Did you try:

pv * (1+r) ** n

?

Jason
  • 2,553
  • 2
  • 9
  • 17