**how to calculate simple interest in Java**and today I am going to share how to calculate compound interest in Java program. Calculating simple interest or compound interest are common coding problem which are taught in schools and colleges to teach you coding. The best thing about these coding exercise in Java are that everybody are familiar with them so you know the concept already and all you need to do is convert that solution into code which is the skill needed to become a coder or programmer.

__Compound Interest Calculator in Java - Example Program__

Here is our complete Java program to calculate compound interest for given amount. You can play with this program in your favorite IDE like IntelliIDEA or Eclipse to learn more about how it works. There is no better way for learning then playing and making changes and eventually writing on your own.import java.util.Scanner; /* * Java Program to calculate compound interest */ public class Main { public static void main(String args[]) { //creating scanner to accept radius of circle Scanner scanner = new Scanner(System.in); System.out.println("Welcome in Java program to calculate compound interest"); System.out.println("Formula for calculating compound interest is 'p*( 1 + r/n ) ^nt' "); System.out.println("Please enter principle :"); int principle = scanner.nextInt(); System.out.println("Please enter number of years :"); int time = scanner.nextInt(); System.out.println("Please enter interest rate (per annum):"); float rate = scanner.nextFloat(); System.out.println("Please enter number of times interest compounds :"); int n = scanner.nextInt(); double amount = compoundInterest(principle, time, rate, n ); System.out.println("Compound interest calculated by Java program is : " + amount); scanner.close(); } /** * Java method to calculate compound interest. This uses formula * p*( 1 + r/n ) ^nt. In Java, the Math.pow() function is used to calculate powers. For example, to calculate 32, we use Math.pow(3,2). After finding amount, we find interest by subtracting principal. * @param p is principle * @param t is time * @param r is rate * @param n is number of times the interest is compounded * */ public static double compoundInterest(int p, int t, float r, int n) { double amount = p * Math.pow(1 + (r / (100*n)), n * t); double interest = amount - p; return interest; } } Output Welcome in Java program to calculate compound interest Formula for calculating compound interest is 'p*( 1 + r/n ) ^nt' Please enter principle : 1500 Please enter number of years : 6 Please enter interest rate (per annum): 4 Please enter number of times interest compounds : 4 Compound interest calculated by Java program is : 404.60154118524906

That's all about **how to write a Java Program to calculate compound interest**. The key thing we learned from this program is choosing right data type e.g. choosing int for interest rate can make the whole calculate wrong because Java would have used int arithmetic instead of floating point arithmetic. Another thing you learn is how to use the Math.pow() method to calculate power and how to convert a mathematical formal like we had for calculating compound interest into a Java program.

**Coding Problems**for Practice

- How to solve the FizzBuzz problem in Java? (solution)
- How to check if a given number is prime or not? (solution)
- Top 10 Programming problems from Java Interviews? (article)
- 7 Best Courses to learn Data Structure and Algorithms (best courses)
- Write code to implement the Quicksort algorithm in Java? (algorithm)
- 100+ Data Structure and Algorithms Problems (solved)
- Write a program to print the highest frequency word from a text file? (solution)
- 10 Books to learn Data Structure and Algorithms (books)
- How to remove duplicate elements from ArrayList in Java? (solution)
- How do you swap two integers without using a temporary variable? (solution)
- How to print the Pyramid pattern in Java? (solution)
- Write a program to check if a number is the power of two or not? (solution)
- How to find if given String is a palindrome in Java? (solution)
- 10 Free Courses to learn Data Structure and Algorithms (courses)
- How to find duplicate characters from String in Java? (solution)
- How do you reverse word of a sentence in Java? (solution)
- How to reverse an int variable in Java? (solution)
- How to find a missing number in a sorted array? (solution)
- How to check if a year is a leap year in Java? (answer)
- How to calculate factorial using recursion and iteration? (solution)
- How to reverse String in Java without using StringBuffer? (solution)
- Write code to implement the Bubble sort algorithm in Java? (code)
- Write a program to code insertion sort algorithm in Java (program)

If you like this coding problem and want to practice more to improve
your coding skill, then you can also search for Java programming
exercises in this blog.

## No comments:

## Post a Comment

Feel free to comment, ask questions if you have any doubt.