Trong bài viết này, chúng ta sẽ nhập số tiền gốc, lãi suất và thời gian (năm) từ người dùng để tính lãi đơn và lãi kép.
Lãi đơn – Phần trăm lãi trên tổng số tiền gốc. Lợi nhuận thấp hơn lãi kép.
Lãi kép – Phần trăm lãi tính trên cả gốc và lãi đã tích lũy. Lãi suất cao hơn lãi đơn.
Đầu vào
Giả sử đầu vào:
Gốc = 25000.0 Lãi suất năm = 10.0 Thời gian (năm) = 4.0
Đầu ra
Kết quả tính lãi đơn và lãi kép:
Lãi đơn: 10000.0 Lãi kép: 11602.500000000007
Thuật toán
Bước 1 – BẮT ĐẦU Bước 2 – Khai báo 5 biến double: p, r, t, s_interest, c_interest Bước 3 – Đọc giá trị p, r, t từ người dùng Bước 4 – Thực hiện (p * r * t) / 100 Bước 5 – Thực hiện p * Math.pow(1.0 + r/100.0, t) - p Bước 6 – Lưu kết quả bước 4 vào s_interest Bước 7 – Lưu kết quả bước 5 vào c_interest Bước 8 – Hiển thị s_interest Bước 9 – Hiển thị c_interest Bước 10 – DỪNG
Ví dụ 1: Nhập từ bàn phím
Người dùng nhập giá trị dựa trên lời nhắc. Thử trực tiếp trong coding ground.
import java.util.*;
public class SimpleAndCompoundInterest {
public static void main(String[] args) {
double p, r, t, s_interest, c_interest;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the value of Principal = ");
p = scanner.nextDouble();
System.out.println("Enter the Annual Rate of Interest = ");
r = scanner.nextDouble();
System.out.println("Enter the Time (years) = ");
t = scanner.nextDouble();
s_interest = (p * r * t) / 100;
c_interest = p * Math.pow(1.0 + r / 100.0, t) - p;
System.out.println("Simple Interest: " + s_interest);
System.out.println("Compound Interest: " + c_interest);
}
}
Đầu ra
Enter the value of Principal = 1500 Enter the Annual Rate of Interest = 8 Enter the Time (years) = 3 Simple Interest: 360.0 Compound Interest: 389.568000000000
Ví dụ 2: Giá trị định nghĩa trước
Giá trị đã được định nghĩa trước, truy xuất và hiển thị.
import java.util.*;
public class SimpleAndCompoundInterest {
public static void main(String[] args) {
double p, r, t, s_interest, c_interest;
p = 25000;
r = 10;
t = 4;
System.out.println("Principal = " + p);
System.out.println("Annual Rate of Interest = " + r);
System.out.println("Time (years) = " + t);
s_interest = (p * r * t) / 100;
c_interest = p * Math.pow(1.0 + r / 100.0, t) - p;
System.out.println("Simple Interest: " + s_interest);
System.out.println("Compound Interest: " + c_interest);
}
}
Đầu ra
Principal = 25000.0 Annual Rate of Interest = 10.0 Time (years) = 4.0 Simple Interest: 10000.0 Compound Interest: 11602.500000000007