public class SimpleInterestCalculator {
// Method to calculate simple interest
public static double calculateSimpleInterest(double principal, double rate, double years) {
if (principal < 0 || rate < 0 || years < 0) {
throw new IllegalArgumentException("Principal, rate, and time must be non-negative.");
}
return (principal * rate * years) / 100;
}
public static void main(String[] args) {
try {
// Input principal amount
double principal = 30000F;
// Input annual interest rate
double rate = 4F;
// Input time in years
double years = 3F;
// Calculate simple interest
double simpleInterest = calculateSimpleInterest(principal, rate, years);
// Display the result
System.out.printf("The Simple Interest is: %.2f\n", simpleInterest);
} catch (Exception e) {
System.out.println("Invalid input. Please enter valid numeric values.");
}
}
}
/*
run:
The Simple Interest is: 3600.00
*/