i have this value and know how to calculate it but i don't know how to put in a recursion code.
Any advice pls...Thank you
the amount $10000
the interest rate 10%
the investment period 10 years
the total value would $25, 937.42
main calls Ryear(10) return 23579.47
n = 10 return 23579.47 * 1.1
n = 9 return 21435.89 * 1.1
n = 8 return 19487.17 * 1.1
n = 7 return 17715.61 * 1.1
n = 6 return 16105.1 * 1.1
n = 5 return 14641 * 1.1
n = 4 return 13310 * 1.1
n = 3 return 12100 * 1.1
n = 2 return 11000 * 1.1
n = 1 return 10000 * 1.1
n = 0 Return 10000
this just is the example i got from my class...
static void Main(string[] args)
{
Console.WriteLine(Ryear(10));
Console.ReadLine();
}
static private int Ryear(int n)
{
int amount = 10000;
int
if (n < 1)
return 0;
else
return Ryear(n-1)+ n;
}
Loading
VulpesPosted Nov 24, 2011, 6:51 AM
Adjusting his code to use recursion, gives us the following:
VulpesPosted Nov 24, 2011, 2:03 PM
Consequently, when you divide it by 100, integer rather than floating point arithmetic will be used and you'll lose some accuracy in the final result.
I'd therefore change amount to double even if you only intend to input integers.
Jonathan CrispePosted Nov 24, 2011, 1:55 PM
its just double and int.
public Form1()
{
InitializeComponent();
}
int m_amount, m_rate, m_years;
double m_total = 0;
private void btnCALCULATE_Click(object sender, EventArgs e)
{
m_amount = int.Parse(tbAMOUNT.Text);
m_rate = int.Parse(tbRATE.Text);
m_years = (int)numYEARS.Value;
m_total = Recursion(m_amount, m_rate, m_years);
tbFUTUREVALUE.Text = m_total.ToString("C2");
}
static double Recursion(int amount, int years, int rate)
{
if (years < 1) return amount;
amount += amount / 100 * rate;
return Recursion(amount, years - 1, rate);
}
}
it works but wrong calculation:
i put the same value: 10000 amount, 10 interest rate, 10 in investment period. the difference is the future value.
the future value : $25, 910.00 instead of $25, 937.42
VulpesPosted Nov 24, 2011, 8:09 AM
Karthik AgarwalPosted Nov 24, 2011, 7:04 AM
Karthik AgarwalPosted Nov 24, 2011, 3:33 AM
Karthik AgarwalPosted Nov 24, 2011, 1:57 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace interest_calculation
{
class Program
{
static void Main(string[] args)
{
while (true)
{
float principal_amount = 0;
int no_of_yrs = 0;
int interest_rate = 0;
float final_amount = 0;
Console.Write("Please Enter the principal amount : ");
principal_amount = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
Console.Write("Please Enter the years you want to deposit for : ");
no_of_yrs = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
Console.Write("Please Enter the interest rate : ");
interest_rate = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
for (int i = 0; i < no_of_yrs; i++)
{
final_amount = principal_amount + ((principal_amount / 100) * interest_rate);
principal_amount = final_amount;
}
Console.WriteLine("Your final returns are : " + Convert.ToString(final_amount));
Console.ReadLine();
}
}
}
}
Jonathan CrispePosted Nov 24, 2011, 1:26 AM
I'm will trying to display the final answer using recursion code...
Any number in principal amount and interest rate.
The range will be 1 to 100, representing the years of investment.
The calculated interest will be added to the principal value once every period (year).
example:
Lets put 10000 in principal amount and 10% in interest rate and
And 10 in years of investment.
The calculation would be this:
10000 * 1.1 = 11000 or ((10000 * 0.1) + 10000)
11000 * 1.1 = 12100 or ((11000 * 0.1) + 11000)
12100 * 1.1 = . . . .
. . . . * 1.1 = 25, 937.42
finally would be $25, 937.42
Karthik AgarwalPosted Nov 24, 2011, 1:01 AM