Chương trình Java để tính lãi đơn giản và lãi kép

Trong ví dụ này, chúng ta sẽ học cách tính lãi đơn và lãi kép trong Java.

Để hiểu ví dụ này, bạn nên có kiến ​​thức về các chủ đề lập trình Java sau:

  • Lớp máy quét Java
  • Toán tử Java

Ví dụ 1: Tính lãi đơn giản trong Java

 import java.util.Scanner; class Main ( public static void main(String() args) ( // create an object of Scanner class Scanner input = new Scanner(System.in); // take input from users System.out.print("Enter the principal: "); double principal = input.nextDouble(); System.out.print("Enter the rate: "); double rate = input.nextDouble(); rate = rate/100; System.out.print("Enter the time: "); double time = input.nextDouble(); double interest = (principal * time * rate) / 100; System.out.println("Principal: " + principal); System.out.println("Interest Rate: " + rate); System.out.println("Time Duration: " + time); System.out.println("Simple Interest: " + interest); input.close(); ) )

Đầu ra

 Nhập tiền gốc: 1000 Nhập lãi suất: 8 Nhập thời gian: 2 Tiền gốc: 1000,0 Lãi suất: 8,0 Thời gian Thời hạn: 2,0 Tiền lãi đơn giản: 160,0

Trong ví dụ trên, chúng tôi đã sử dụng Scannerlớp để lấy hiệu trưởng , xếp hạngthời gian làm đầu vào từ người dùng. Sau đó, chúng tôi sử dụng công thức của lãi suất đơn giản để tính lãi suất đơn giản.

 Simple Interest = (Principal * Rate * Time) / 100

Ví dụ 2: Tính lãi kép

 import java.util.Scanner; class Main ( public static void main(String() args) ( // create an object of Scanner class Scanner input = new Scanner(System.in); // take input from users System.out.print("Enter the principal: "); double principal = input.nextDouble(); System.out.print("Enter the rate: "); double rate = input.nextDouble(); System.out.print("Enter the time: "); double time = input.nextDouble(); System.out.print("Enter number of times interest is compounded: "); int number = input.nextInt(); double interest = principal * (Math.pow((1 + rate/100), (time * number))) - principal; System.out.println("Principal: " + principal); System.out.println("Interest Rate: " + rate); System.out.println("Time Duration: " + time); System.out.println("Number of Time interest Compounded: " + number); System.out.println("Compound Interest: " + interest); input.close(); ) )

Đầu ra

 Nhập tiền gốc: 1000 Nhập lãi suất: 10 Nhập thời gian: 3 Nhập số lần lãi gộp: 1 Tiền gốc: 1000,0 Lãi suất: 10,0 Thời gian Thời hạn: 3,0 Số lần Lãi gộp: 1 Lãi gộp: 331.00000000000045

Trong ví dụ trên, chúng ta đã sử dụng công thức lãi kép để tính lãi kép.

Ở đây, chúng tôi đã sử dụng phương thức Math.pow () để tính lũy thừa của một số.

thú vị bài viết...