Write a Program to Calculate Simple Interest in C#

Do you want to know how to calculate simple interest in C#? In this C# tutorial, I will show you how to write a program to calculate simple interest in C#.

To calculate simple interest in C#, you can create a method that takes principal, rate, and time as inputs and returns the interest using the formula (principal * rate * time) / 100. You can then prompt the user to enter these values, call this method, and display the result.

Simple interest is computed using the formula:

Simple Interest=(Principal√óRate√óTime)/100

Where:

  • Principal is the original sum of money borrowed or invested.
  • Rate is the interest rate per period.
  • Time is the time the money is borrowed or invested, usually in years.

Calculate Simple Interest in C#

Now, let us see how to write a program to calculate simple interest in C#.

Here is the complete code:

using System;

namespace SimpleInterestCalculator
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Simple Interest Calculator in C#");

            try
            {
                // Input for Principal amount
                Console.Write("Enter the Principal amount: ");
                decimal principal = Convert.ToDecimal(Console.ReadLine());

                // Input for Rate of interest
                Console.Write("Enter the Rate of interest (in %): ");
                decimal rate = Convert.ToDecimal(Console.ReadLine());

                // Input for Time period
                Console.Write("Enter the Time period (in years): ");
                decimal time = Convert.ToDecimal(Console.ReadLine());

                // Calculating simple interest
                decimal simpleInterest = CalculateSimpleInterest(principal, rate, time);

                // Displaying the result
                Console.WriteLine($"The Simple Interest is: {simpleInterest:C2}");
            }
            catch (FormatException)
            {
                Console.WriteLine("Invalid input. Please enter a valid number.");
            }

            Console.ReadKey();
        }

        static decimal CalculateSimpleInterest(decimal principal, decimal rate, decimal time)
        {
            return (principal * rate * time) / 100;
        }
    }
}

Here is the code explanation:

  1. Namespace and Class Declaration: We declare a namespace SimpleInterestCalculator and a class Program.
  2. Main Method: This is the entry point of our C# program.
  3. Taking User Input: We prompt the user to enter the principal amount, rate of interest, and time period.
  4. Error Handling: We use a try-catch block to handle any format exceptions (like entering a non-numeric value).
  5. Calculating Simple Interest: We define a method CalculateSimpleInterest that takes the principal, rate, and time as parameters and returns the calculated simple interest.
  6. Displaying the Result: The simple interest is displayed in currency format.

Once you run the program using the Visual Studio Console application, you will be prompted to enter the principal amount, rate of interest, and time period. The program will then display the calculated simple interest.

You can see the output in the screenshot below:

write a program to calculate simple interest in C#

Conclusion

You have successfully created a C# program to calculate simple interest. This program demonstrates basic C# syntax, user input handling, and simple mathematical calculations. I hope now you have a very easy and simple method to calculate simple interest in C#.

You may like the following: