
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Program to Calculate Simple Interest
Simple Interest is the interest calculated only on the initial principal amount taken. This is not like compound interest where interest is compounded, i.e., previous interest is included in the current time period. This interest is simple and grows linearly over time.
In this article, we will discuss how to calculate simple interest using C#.
The formula to calculate Simple Interest is:
Simple Interest (SI) = P Ã R Ã T / 100
Where:
- p is the principal amount
- r is the interest rate
- t is the time period in years
Example
Input:
principal = 10,000
rate = 10
years = 2
Output:
Simple Interest = 2000
Input:
principal = 3,000
rate = 5
years = 2
Output:
Simple Interest = 300
Calculating Simple Interest Using Direct Formula
We first take principal as p
, rate as r
, and time period as t
. Now we use the formula for calculating simple interest. After calculating, we return the result. The formula for Simple Interest is:
Simple Interest = P Ã R Ã T / 100
Steps for Implementation
- We take input for principal, rate, and time.
- Apply the formula for calculating simple interest:
SI = P Ã R Ã T / 100
. - Return the calculated simple interest.
Implementation Code
using System; class Program { static void Main(string[] args) { // Take the Principal amount double principal = 10000; // Annual rate of interest double rate = 5; // Time in years double time = 2; // Calculate the Simple Interest double simple_Interest = (principal * rate * time) / 100; // Output Console.WriteLine("The Simple Interest is: {0}", simple_Interest); } }
Output:
The Simple Interest is: 1000
Time Complexity: O(1), constant time
Space Complexity: O(1), constant space
Calculating Simple Interest Using Iterative Approach
We can use an iterative approach to calculate simple interest. This involves manually adding the interest for each year in a loop. After the loop completes, we return the total simple interest.
Steps for Implementation
- We first take input as principal, rate, and time period.
- Use a loop to calculate the interest year by year and add the interest for each year.
- Return the total simple interest.
Implementation Code
using System; class Program { static void Main(string[] args) { // Input values double principal = 10000; double rate = 5; double time = 2; // Calculate simple interest using loop double total_Interest = 0; for (int i = 0; i < time; i++) { total_Interest += (principal * rate) / 100; } // Output the result Console.WriteLine($"The Simple Interest is: {total_Interest}"); } }
Output:
The Simple Interest is: 1000
Time Complexity: O(n), as we are using a loop to traverse for each year
Space Complexity: O(1), constant space