From 1190db594ac889a02c4b39b9cd6db8d954c5528c Mon Sep 17 00:00:00 2001 From: kapilrohilla Date: Mon, 2 Oct 2023 12:36:09 +0530 Subject: [PATCH] Add java program to calculate Simple Interest --- src/navs/program.js | 1 + .../programs/calculate-simple-interest.mdx | 69 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 src/pages/programs/calculate-simple-interest.mdx diff --git a/src/navs/program.js b/src/navs/program.js index 5de23cf8..48da2467 100644 --- a/src/navs/program.js +++ b/src/navs/program.js @@ -15,5 +15,6 @@ export const programsNav = { pages['java-program-to-add-two-complex-numbers'], pages['multiply-two-numbers'], pages['java-program-to-check-Leap-year'], + pages['calculate-simple-interest'], ], } diff --git a/src/pages/programs/calculate-simple-interest.mdx b/src/pages/programs/calculate-simple-interest.mdx new file mode 100644 index 00000000..228452a8 --- /dev/null +++ b/src/pages/programs/calculate-simple-interest.mdx @@ -0,0 +1,69 @@ +--- +title: Java Program to Calculate Simple interest +shotTitle: Calculate Simple Interest +description: In this program, you'll learn to calculate the simple interest and display final value on screen/terminal +--- +import {TipInfo} from '@/components/Tip' + +To understand this example, you should have the knowledge of the following Java programming topics: + +- [Java Operators](/docs/operators) +- [Java Basic Input and Output](/docs/basic-input-output) + +## Calculate Simple Interest + +A java program that calculate the simple interest by getting user input for priniple amount, Rate of interest, Time Period. + +```java +import java.util.Scanner; +public class simpleInterest { + public static void main(String[] args){ + int principleAmount,time; + float interestRatePerAnnum; + // instance of Scanner class + Scanner input = new Scanner(System.in); + // taking user input + System.out.print("Enter the principle amount : "); + principleAmount = input.nextInt(); + System.out.print("Enter the rate of interest(per annum): "); + interestRatePerAnnum = input.nextFloat(); + System.out.print("Enter the time period(y): "); + time = input.nextInt(); + + float simpleInterest = (principleAmount * interestRatePerAnnum * time)/100; + + System.out.println("Simple Interest: " + simpleInterest); + + float totalAmountToReceive = principleAmount + simpleInterest; + System.out.println("Total amount person receive after " + time + " year" + ": " + totalAmountToReceive); + } +} +``` + +#### Output + +```text +Enter the principle amount : 1500 +Enter the rate of interest(per annum): 8 +Enter the time period(y): 5 +Simple Interest: 600.0 +Total amount person receive after 5 year: 2100. +``` + +To Calculate Simple interest , +```SimpleInterest = (P*R*T)/100``` + + +here, P = principleAmount + R = Rate of interest (%) + T = Time Period (per annum), + +We need to take user input for following (P,R,T) and then apply the formulae using ```*,/``` operator. And then, print the output to user's terminal using ```println()``` funciton. + + + +Don't know how to take input from the user ? Look at [this examples](/docs/basic-input-output#java-input) + +Here two input numbers are taken from user one after another with space in between them which distinguish between two different inputs, this useful behavior is because of the default settings of Scanner called as Delimiter, [learn more here](https://www.javatpoint.com/post/java-scanner-delimiter-method). + +