-
-
Notifications
You must be signed in to change notification settings - Fork 13
Added Compound Interest Java Code #570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
--- | ||
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' | ||
|
||
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 | ||
|
||
```java | ||
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(); | ||
} | ||
} | ||
``` | ||
|
||
#### 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 | ||
|
||
```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 | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.