Java Program - Simple Interest Calculator¶
Concept Explanation¶
What is it?¶
Simple interest is a quick and easy method of calculating the interest charge on a loan or investment. It is determined by multiplying the daily interest rate by the principal by the number of days that elapse between payments.
Formula¶
$\(SI = \frac{P \times R \times T}{100}\)$ Where: - P = Principal amount - R = Rate of interest per annum - T = Time (in years)
Algorithm¶
- Start
- Declare variables for Principal (
p), Rate (r), Time (t), and Interest (si). - Accept or initialize input values.
- Apply the formula:
si = (p * r * t) / 100. - Display the result.
- End
Implementations¶
import java.util.Scanner;
public class SimpleInterest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Principal Amount: ");
double p = sc.nextDouble();
System.out.print("Enter Annual Interest Rate (%): ");
double r = sc.nextDouble();
System.out.print("Enter Time (Years): ");
double t = sc.nextDouble();
double si = (p * r * t) / 100;
System.out.println("-------------------");
System.out.println("Simple Interest: " + si);
System.out.println("Total Amount: " + (p + si));
sc.close();
}
}
Explanation¶
- Java
- Uses
java.util.Scannerfor user input. - Uses
doubledata type to handle decimal values for precision. - Ensures the formula order follows PEMDAS.
Complexity Analysis¶
- Time Complexity: O(1)
- Space Complexity: O(1)
Sample Dry Run¶
| Principal | Rate | Time | Calculation | Output |
|---|---|---|---|---|
| 1000 | 5 | 2 | (1000 * 5 * 2) / 100 | 100 |
Related Concepts¶
"Interest on debts is inventory's cousin." - Anonymous