Calculate Simple Interest

Submitted by:Jhon Smith

Date added:13 June, 2011

Category:C#

This C# code snippet inputs the principal and interest amounts and calculates simple interest.

Tags: calculate interest

Code Snippet:

using System;

public class CalculateSimpleInterest
{
public static void Main (string[] args)
{
Console.Write ("Enter principal amount: ");
decimal principal = Convert.ToDecimal (Console.ReadLine());
if (principal < 0)
{ // principal cannot be negative
Console.WriteLine ("Principal cannot be negative");
principal = 0;
}

Console.Write ("Enter interest rate : ");
decimal interest = Convert.ToDecimal (Console.ReadLine());
if (interest < 0)
{ // interest cannot be negative
Console.WriteLine ("Interest cannot be negative");
interest = 0;
}

decimal interestPaid = principal * (interest / 100);
decimal totalPI = principal + interestPaid;
Console.WriteLine ("\nPrincipal = " + principal
+ "\nInterest = " + interest + "%"
+ "\nInterest paid = " + interestPaid
+ "\nTotal (P+I) = " + totalPI);

Console.WriteLine ("\nPress Enter to stop");
Console.Read();
}
}
 
 

Comments