Advertisements

Aatul Palandurkar

Simple Interest Example in Java

Posted on: July 20, 2015

Starting a series of some Java programs as many students were asking to share the programs. Here is the first program of the series.

Objectives:

  • Write a Program that calculates and prints the simple interest using the formula : Simple Interest = PTR/100
  • How to calculate Simple Interest in Java?
  • Write a program to calculate Simple Interest in Java.
  • Write a Java program that calculates and prints the simple interest using the formula : “SimpleInterest = PTR/100” and input values P, T, R should be accepted as command line.
  • How to use Command Line Arguments in Java?
  • How to pass values from command line in Java?
  • How to take input in Java from console?

 

This particular program can be written in various ways, we will try to write the solution program in different ways here and one can try to understand the difference between them.

Method 1 :

import java.util.Scanner;

public class SimpleInterestExample {

    public static void main(String[] args) {

        int p,t,r, result;

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the Value of P : ");
        p = sc.nextInt();

        System.out.println("Enter the Value of T : ");
        t = sc.nextInt();

        System.out.println("Enter the Value of R : ");
        r = sc.nextInt();

        result = (p*t*r)/100;
        System.out.println("Interest is : " + result);
    }
}

This one is the simplest way to write the program to find simple interest in Java. Here we have simply created the object of Scanner class for taking input. Scanner class was introduced in Java 6. To use Scanner class we will have to import the class from java.util package.

Method 2:

Now we will write a Java program that calculates and prints the simple interest using the Scanner class again but with object oriented programming approach.

import java.util.Scanner;

public class SimpleInterestExample
{
		double principalAmount = 0;
        double interestRate = 0;
        double term = 0;
		double simpleInterest = 0; 

    public void calculateSimpleInterest()
	{
		Scanner input = new Scanner(System.in);

		System.out.print("Enter the Principal amount : ");
		principalAmount = input.nextDouble();

		System.out.print("Enter the Rate As a decimal : ");
		interestRate = input.nextDouble();

		System.out.print("Enter the amount of time in years : ");
		term = input.nextDouble();

		simpleInterest = (principalAmount * interestRate * term) / 100;
	}

    public void displaySimpleInterest()
	{
		System.out.println("The Simple Interest is : " + simpleInterest);
    }
}

Mock Test Program for above code so that we can test the code. keep both the classes in same package and run the Mock Test Program or MockTestProgram.java

public class MockTestProgram{

    public static void main(String[]args);
	{
		SimpleInterestExample simpleInt = new SimpleInterestExample();
		simpleInt.calculateSimpleInterest();
		simpleInt.displaySimpleInterest();
	}
}

Method 3:

Now we will write a Java program that calculates and prints the simple interest using the formula : “SimpleInterest = PTR/100” and input values P, T, R should be accepted as command line.

import java.util.Scanner;

public class SimpleInterestExample {

    public static void main(String[] args) {
		double p,t,r, result;

		p = Double.parseDouble(args[0]);
		t = Double.parseDouble(args[1]);
		r = Double.parseDouble(args[2]);

		Scanner sc = new Scanner(System.in);

		System.out.println("Enter the Value of P : ");
		p = sc.nextDouble();

		System.out.println("Enter the Value of T : ");
		t = sc.nextDouble();

		System.out.println("Enter the Value of R : ");
		r = sc.nextDouble();

		result = (p*t*r)/100;
		System.out.println("Interest is : " + result);
    }
}

 

I will be sharing more programs soon as many students were asking for programs which were carried out during Java training and were asked in assignments after Java training. By the time enjoy coding in Java and use NetBeans, it will ease your life.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Advertisements

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 8,305 other followers

Tweets

%d bloggers like this: