Trong bài viết này, chúng ta sẽ tìm hiểu cách tính lãi đơn trong Java. Lãi đơn được tính bằng công thức: Nếu Gốc = P, Lãi suất = R% mỗi năm, Thời gian = T năm:
Lãi đơn (S.I) = P * T * R / 100
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.
Dưới đây là minh họa:
Đầu vào
Giả sử đầu vào:
Nhập số tiền gốc: 100000 Nhập lãi suất: 5 Nhập thời gian (năm): 2
Đầu ra
Kết quả mong đợi:
Lãi đơn: 1000
Thuật toán
Bước 1 – BẮT ĐẦU Bước 2 – Khai báo 4 biến float: principle, rate, time, simple_interest Bước 3 – Đọc giá trị principle, rate, time từ người dùng Bước 4 – Thực hiện: (principle * rate * time) / 100 để tính lãi đơn, lưu vào simple_interest Bước 5 - Hiển thị simple_interest Bước 6 – DỪNG
Ví dụ 1: Nhập từ bàn phím
Người dùng nhập dựa trên lời nhắc. Thử trực tiếp trong coding ground.
import java.util.Scanner;
public class SimpleInterest {
public static void main(String args[]) {
float principle, rate, time, simple_interest;
System.out.println("Required packages have been imported");
Scanner my_scanner = new Scanner(System.in);
System.out.println("A my_scanner object has been defined");
System.out.print("Enter a Principle number: ");
principle = my_scanner.nextInt();
System.out.print("Enter a Interest rate: ");
rate = my_scanner.nextInt();
System.out.print("Enter a Time period in years: ");
time = my_scanner.nextInt();
simple_interest = (principle * rate * time) / 100;
System.out.println("The Simple Interest is: " + simple_interest);
}
}
Đầu ra
Required packages have been imported A Scanner object has been defined Enter a Principle number: 10000 Enter a Interest rate: 5 Enter a Time period in years: 2 The Simple Interest is: 1000.0
Ví dụ 2: Giá trị định nghĩa trước
Các giá trị đã được định nghĩa trước.
public class SimpleInterest {
public static void main(String args[]) {
float principle, rate, time, simple_interest;
principle = 100000;
rate = 5;
time = 2;
System.out.printf("The Principle amount is %f \nThe interest rate is %f \nThe time period in years is %f", principle, rate, time);
simple_interest = (principle * rate * time) / 100;
System.out.println("\nThe Simple Interest is: " + simple_interest);
}
}
Đầu ra
The Principle amount is 100000.000000 The interest rate is 5.000000 The time period in years is 2.000000 The Simple Interest is: 1000.0