Topics Index

Methods - Importance >
Siva Nookala - 12 Apr 2016
This program explains how to calculate simple interest using methods. The formula for calculating simple interest is I = P * T * R / 100.0 where I is interest, P is principal, T is time in years and R is interest rate per annum.

Calculate Simple Interest Using Methods
class CalculateSimpleInterest
    public static void main(String s[])
        double principal = 5000; // Rs. 5000
        double time = 2; // 2 years
        double rate = 11.25; // 11.25 % per annum
        double interest = calculateInterest( principal, time, rate ); // LINE A
        printSummary( principal, time, rate, interest );
    public static double calculateInterest(double principal, double time, double rate) // LINE B
        double result = principal * time * rate / 100.0; // LINE C
        return result;
    public static void printSummary(double p, double t, double r, double interest)
        System.out.print("Interest for Rs " + p + " for " + t + " years ");
        System.out.print("at the rate of " + r + "% p.a. is Rs " + interest);

Interest for Rs 5000.0 for 2.0 years at the rate of 11.25% p.a. is Rs 1125.0


This program shows how to calculate simple interest using methods. At LINE A, we are calling the calculateInterest method by passing the parameters principal, time and rate. The return value from the method is assigned to variable interest. In the calculateInterest method, we are calculating the interest using the passed parameters. The calculated interest is returned back using the return keyword. The return value is assigned to the variable interest in the main method.
Here the calculateInterest method is the called method, where as the main method is the calling method.
We also have another method printSummary which takes the parameters p, t, r and interest and prints the summary of the interest calculation. As shown in this method, it is not necessary to have the same parameter name in the calling method and the called method. In the calling method which is main, the variable is principal, where as in the called method printSummary it is p. Similarly time is t and rate is r.

  • In LINE B, change the parameter names in calculateInterest method to pr, ti and ra respectively. Since the parameter names are changed we also need to change the variables in LINE C, otherwise it will cause 'undefined variable' compilation error.

Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App