C# Program to Find Compound Interest
Posted by Superadmin on August 15 2022 15:25:20

C# Program to Find Compound Interest

 

 

 

This is a C# Program to calculate compound interest.

Problem Description

This C# Program Calculates Compound Interest.

Problem Solution

Compounding of interest allows a principal amount to grow at a faster rate than simple interest, which is calculated as a percentage of only the principal amount.

Program/Source Code

Here is source code of the C# Program to Calculate Compound Interest. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Calculate Compound Interest
 */
using System;
namespace compund
{
    class compound
    {
        static void Main(string[] args)
        {
            double Total = 0, interestRate, years, annualCompound, Amount;
            Console.Write("Enter the Initial Amount : ");
            Amount = Convert.ToDouble(Console.ReadLine());
            Console.Write("Enter the Rate of Interest : ");
            interestRate = Convert.ToDouble(Console.ReadLine()) / 100;
            Console.Write("Enter the Number of Years : ");
            years = Convert.ToDouble(Console.ReadLine());
            Console.Write("Number of Times the Interest will be Compounded : ");
            annualCompound = Convert.ToDouble(Console.ReadLine());
            for (int t = 1; t < years + 1; t++)
            {
                Total = Amount * Math.Pow((1 + interestRate / annualCompound), 
                                         (annualCompound * t));
                Console.Write("Your Total for Year {0} "
                            + "is {1:F0}. \n", t, Total);
 
            }
 
            Console.ReadLine();
        }
    }
}
Program Explanation

In this C# program, library function defined in <math.h> header file is used. We are reading the initial amount, the rate of interest, and the number of years using ‘Amount’, ‘InterestRate’, ‘Years’ variables respectively. For loop is used to compute the compound interest using the following formula:
Compound Interest = Principal amount (1 + InterestRate/Years) ^ (Years * number of times).

 
Runtime Test Cases
 
Enter the Initial Amount : 1000
Enter the Rate of Interest : 2
Enter the Number of Years : 2
Number of Times the Interest will be Compounded : 2
Your Total for Year 1 is : 1020
Your Total for Year 2 is : 1041