|
| 1 | +--- |
| 2 | +title: Java program To Calculate Compound Interest |
| 3 | +shotTitle: Calculate Compound Interest |
| 4 | +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. |
| 5 | +--- |
| 6 | + |
| 7 | +import { List, ListItemGood } from '@/components/List' |
| 8 | + |
| 9 | +The integer is stored in a variable using `System.in`, and is displayed on the screen using `System.out`. |
| 10 | + |
| 11 | +To understand this example, you should have the knowledge of the following Java programming topics: |
| 12 | + |
| 13 | +- [Java Operators](/docs/operators) |
| 14 | +- [Java Basic Input and Output](/docs/basic-input-output) |
| 15 | + |
| 16 | +## to calculate the compound Interest |
| 17 | + |
| 18 | +A java program to calculate compound Interest |
| 19 | + |
| 20 | + |
| 21 | +## Java Code |
| 22 | + |
| 23 | +```java |
| 24 | +import java.util.Scanner; |
| 25 | + |
| 26 | +public class CompoundInterestCalculator { |
| 27 | + public static void main(String[] args) { |
| 28 | + Scanner scanner = new Scanner(System.in); |
| 29 | + |
| 30 | + // Input principal amount |
| 31 | + System.out.print("Enter the principal amount: "); |
| 32 | + double principal = scanner.nextDouble(); |
| 33 | + |
| 34 | + // Input annual interest rate (as a percentage) |
| 35 | + System.out.print("Enter the annual interest rate (as a percentage): "); |
| 36 | + double annualRate = scanner.nextDouble(); |
| 37 | + |
| 38 | + // Input number of years |
| 39 | + System.out.print("Enter the number of years: "); |
| 40 | + int years = scanner.nextInt(); |
| 41 | + |
| 42 | + // Input number of times interest is compounded per year |
| 43 | + System.out.print("Enter the number of times interest is compounded per year: "); |
| 44 | + int compoundingFrequency = scanner.nextInt(); |
| 45 | + |
| 46 | + // Convert annual rate to decimal and calculate compound interest |
| 47 | + double rate = annualRate / 100; |
| 48 | + double amount = principal * Math.pow(1 + (rate / compoundingFrequency), compoundingFrequency * years); |
| 49 | + |
| 50 | + // Calculate compound interest |
| 51 | + double compoundInterest = amount - principal; |
| 52 | + |
| 53 | + // Display the result |
| 54 | + System.out.println("The compound interest after " + years + " years is: " + compoundInterest); |
| 55 | + |
| 56 | + // Close the scanner |
| 57 | + scanner.close(); |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +#### Output 1 |
| 63 | + |
| 64 | +```text |
| 65 | +Enter the principal amount: 5000 |
| 66 | +Enter the annual interest rate (as a percentage): 5 |
| 67 | +Enter the number of years: 3 |
| 68 | +Enter the number of times interest is compounded per year: 4 |
| 69 | +The compound interest after 3 years is: 797.1955807499652 |
| 70 | +``` |
| 71 | + |
| 72 | +#### Output 2 |
| 73 | + |
| 74 | +```text |
| 75 | +Enter the principal amount: 10000 |
| 76 | +Enter the annual interest rate (as a percentage): 3.5 |
| 77 | +Enter the number of years: 5 |
| 78 | +Enter the number of times interest is compounded per year: 12 |
| 79 | +The compound interest after 5 years is: 1938.8365362833173 |
| 80 | +``` |
0 commit comments