From 370768f3d934bdb23ef9fcd8538c0dbd46bec1ca Mon Sep 17 00:00:00 2001 From: Aman <84791435+Aman28801@users.noreply.github.com> Date: Sat, 7 Oct 2023 03:00:42 +0530 Subject: [PATCH 1/3] Added Compound Interest Code --- src/navs/program.js | 1 + .../programs/calculate-compound-interest.mdx | 66 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/pages/programs/calculate-compound-interest.mdx diff --git a/src/navs/program.js b/src/navs/program.js index 323ed8f2..643cf056 100644 --- a/src/navs/program.js +++ b/src/navs/program.js @@ -19,5 +19,6 @@ export const programsNav = { pages['java-program-to-check-divisbility'], pages['find-quotient-and-reminder'], pages['calculate-power-of-a-number'], + pages['calculate-compound-interest'], ], } diff --git a/src/pages/programs/calculate-compound-interest.mdx b/src/pages/programs/calculate-compound-interest.mdx new file mode 100644 index 00000000..5556a5d3 --- /dev/null +++ b/src/pages/programs/calculate-compound-interest.mdx @@ -0,0 +1,66 @@ +=== +Title = To Calculate Compound Interest +Description = This code takes the principal amount, annual interest rate (as a percentage), + number of years, and compounding frequency (how many times per year the interest is compounded) + as input from the user. + +##Java Code + +import java.util.Scanner; + +public class CompoundInterestCalculator { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + // Input principal amount + System.out.print("Enter the principal amount: "); + double principal = scanner.nextDouble(); + + // Input annual interest rate (as a percentage) + System.out.print("Enter the annual interest rate (as a percentage): "); + double annualRate = scanner.nextDouble(); + + // Input number of years + System.out.print("Enter the number of years: "); + int years = scanner.nextInt(); + + // Input number of times interest is compounded per year + System.out.print("Enter the number of times interest is compounded per year: "); + int compoundingFrequency = scanner.nextInt(); + + // Convert annual rate to decimal and calculate compound interest + double rate = annualRate / 100; + double amount = principal * Math.pow(1 + (rate / compoundingFrequency), compoundingFrequency * years); + + // Calculate compound interest + double compoundInterest = amount - principal; + + // Display the result + System.out.println("The compound interest after " + years + " years is: " + compoundInterest); + + // Close the scanner + scanner.close(); + } +} + +======= Code Ends Here ======= + + +======= Output 1 ====== + +Enter the principal amount: 5000 +Enter the annual interest rate (as a percentage): 5 +Enter the number of years: 3 +Enter the number of times interest is compounded per year: 4 +The compound interest after 3 years is: 797.1955807499652 + + +======= Output 2 ====== + +Enter the principal amount: 10000 +Enter the annual interest rate (as a percentage): 3.5 +Enter the number of years: 5 +Enter the number of times interest is compounded per year: 12 +The compound interest after 5 years is: 1938.8365362833173 + + From b54cb0d725634c2eb1ce463f50da2df33af396f6 Mon Sep 17 00:00:00 2001 From: Aman <84791435+Aman28801@users.noreply.github.com> Date: Sun, 8 Oct 2023 17:17:02 +0530 Subject: [PATCH 2/3] Changes --- .../programs/calculate-compound-interest.mdx | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/pages/programs/calculate-compound-interest.mdx b/src/pages/programs/calculate-compound-interest.mdx index 5556a5d3..3d644ec5 100644 --- a/src/pages/programs/calculate-compound-interest.mdx +++ b/src/pages/programs/calculate-compound-interest.mdx @@ -3,6 +3,21 @@ Title = To Calculate Compound Interest Description = This code takes the principal amount, annual interest rate (as a percentage), number of years, and compounding frequency (how many times per year the interest is compounded) as input from the user. +=== + +import { List, ListItemGood } from '@/components/List' + +The integer is stored in a variable using `System.in`, and is displayed on the screen using `System.out`. + +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) + +## to calculate the compound Interest + +A java program to calculate compound Interest + ##Java Code From fb1b022e38c26de7f2e2373bc22cc16405caf52e Mon Sep 17 00:00:00 2001 From: Arghya Ghosh Date: Sun, 8 Oct 2023 22:48:37 +0530 Subject: [PATCH 3/3] Update calculate-compound-interest.mdx --- .../programs/calculate-compound-interest.mdx | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/pages/programs/calculate-compound-interest.mdx b/src/pages/programs/calculate-compound-interest.mdx index 3d644ec5..f611baf5 100644 --- a/src/pages/programs/calculate-compound-interest.mdx +++ b/src/pages/programs/calculate-compound-interest.mdx @@ -1,9 +1,8 @@ -=== -Title = To Calculate Compound Interest -Description = This code takes the principal amount, annual interest rate (as a percentage), - number of years, and compounding frequency (how many times per year the interest is compounded) - as input from the user. -=== +--- +title: Java program To Calculate Compound Interest +shotTitle: Calculate Compound Interest +description: This code takes the principal amount, annual interest rate (as a percentage), number of years, and compounding frequency (how many times per year the interest is compounded) as input from the user. +--- import { List, ListItemGood } from '@/components/List' @@ -19,8 +18,9 @@ To understand this example, you should have the knowledge of the following Java A java program to calculate compound Interest -##Java Code +## Java Code +```java import java.util.Scanner; public class CompoundInterestCalculator { @@ -57,25 +57,24 @@ public class CompoundInterestCalculator { scanner.close(); } } +``` -======= Code Ends Here ======= - - -======= Output 1 ====== +#### Output 1 +```text Enter the principal amount: 5000 Enter the annual interest rate (as a percentage): 5 Enter the number of years: 3 Enter the number of times interest is compounded per year: 4 The compound interest after 3 years is: 797.1955807499652 +``` +#### Output 2 -======= Output 2 ====== - +```text Enter the principal amount: 10000 Enter the annual interest rate (as a percentage): 3.5 Enter the number of years: 5 Enter the number of times interest is compounded per year: 12 The compound interest after 5 years is: 1938.8365362833173 - - +```