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:
- Namespace and Class Declaration: We declare a namespace
SimpleInterestCalculator
and a classProgram
. - Main Method: This is the entry point of our C# program.
- Taking User Input: We prompt the user to enter the principal amount, rate of interest, and time period.
- Error Handling: We use a
try-catch
block to handle any format exceptions (like entering a non-numeric value). - Calculating Simple Interest: We define a method
CalculateSimpleInterest
that takes the principal, rate, and time as parameters and returns the calculated simple interest. - 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:
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:
- Write A Program To Find Second Largest Number In An Array In C#
- Write A C# Program To Reverse The Words Of A Sentence
- Write A C# Program To Check Palindrome Number
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…