C# programs to calculate compound interest have been shown here. Both of the iterative and recursive approaches have been covered. Computing compound interest using formula has also been shown here.
The following section covers the C# programs to calculate compound interest.
Page content(s):
2. Program: Iterative approach
3. Program: Recursive approach
Additional content(s):
1. C# Program & output to calculate compound interest using formula
/***************************************************** alphabetacoder.com C# program to calculate compound interest using formula ******************************************************/ using System; namespace CompoundInterest { class Program { static void Main(string[] args) { // declare variables double ci, amount, p, r; int n, t; // take input of initial principal amount, // interest rate, periodicity of payment and year Console.Write("Enter the principal balance = "); p = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter interest rate= "); r = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter compound frequency / year = "); n = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter the year = "); t = Convert.ToInt32(Console.ReadLine()); // calculate compounded value // using formula amount = p * Math.Pow((1 + r/(100 * n)), n*t); //find the compound interest ci = amount - p; // display result upto 2 decimal places Console.WriteLine("Compound interest = "+Math.Round(ci, 2)); // wait for user to press any key Console.ReadKey(); } } }
Output
Enter the principal balance = 7200
Enter the annual interest rate = 6
Enter compound frequency / year = 2
Enter the year = 5
Compound interest = 2476.2
2. C# Program & output to calculate compound interest using loop (Iteration)
/************************************************************** alphabetacoder.com C# program to calculate compound interest using loop (Iteration) ***************************************************************/ using System; namespace CompoundInterest { class Program { static void Main(string[] args) { // declare variables double ci, amount, p, r; int n, t, i; // take input of initial principal amount, // interest rate, periodicity of payment and year Console.Write("Enter the principal balance = "); p = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter interest rate= "); r = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter compound frequency / year = "); n = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter the year = "); t = Convert.ToInt32(Console.ReadLine()); // initialize amount = p; // calculate compounded value using loop for(i = 0; i < n * t; i++) amount = amount + amount * (r / (n * 100)); //find the compound interest ci = amount - p; // display result upto 2 decimal places Console.WriteLine("Compound interest = "+Math.Round(ci, 2)); // wait for user to press any key Console.ReadKey(); } } }
Output
Enter the principal balance = 35000
Enter the annual interest rate = 5.2
Enter compound frequency / year = 1
Enter the year = 10
Compound interest = 23106.6
3. C# Program & output to calculate compound interest using recursion
/********************************************************** alphabetacoder.com C# program to calculate compound interest using recursion ************************************************************/ using System; namespace CompoundInterest { class Program { // recursive function to compute compound interest static double compound_interest(double p0, double p, double r, int n, int t, int itr){ // if number of total no of compound reached if(itr == n * t) return (p - p0); // calculate interest double interest = p * (r / (n * 100)); // call function return compound_interest(p0, p + interest, r, n, t, itr + 1); } static void Main(string[] args) { // declare variables double ci, p, r; int n, t; // take input of initial principal amount, // interest rate, periodicity of payment and year Console.Write("Enter the principal balance = "); p = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter interest rate= "); r = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter compound frequency / year = "); n = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter the year = "); t = Convert.ToInt32(Console.ReadLine()); // call function to compute compound interest ci = compound_interest(p, p, r, n, t, 0); // display result upto 2 decimal places Console.WriteLine("Compound interest = "+Math.Round(ci, 2)); // wait for user to press any key Console.ReadKey(); } } }
Output
Enter the principal balance = 90000
Enter the interest rate = 7
Enter compound frequency / year = 4
Enter the year = 20
Compound interest = 270575.27