Calculate simple interest

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
C# Code Snippets

C# Source Code Bank

See also …
edit

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

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();
   }
}


 Calculate Simple Interest (program output)
Enter principal amount: 1000

Enter interest rate  : 15


Principal = 1000 Interest = 15% Interest paid = 150.00 Total (P+I) = 1150.00

See also



Personal tools