Computer >> 컴퓨터 >  >> 프로그래밍 >> Python

원금·기간·이율이 주어졌을 때 단순 이자를 계산하는 Python 프로그램

원금(principal), 기간(time), 이율(rate)과 같은 필수 값들이 주어졌을 때 단순 이자(Simple Interest)를 계산해야 하는 경우가 종종 있습니다. 이때는 간단한 공식을 정의하고, 입력받은 값들을 공식에 대입하기만 하면 됩니다.

단순 이자의 기본 공식은 다음과 같습니다.

단순 이자 = (원금 × 기간 × 이율) / 100

예제 코드

principle_amt = float(input("Enter the principle amount..."))
my_time = int(input("Enter the time in years..."))
my_rate = float(input("Enter the rate..."))
my_simple_interest=(principle_amt*my_time*my_rate)/100
print("The computed simple interest is :")
print(my_simple_interest)

실행 결과

Enter the principle amount...45000
Enter the time in years...3
Enter the rate...6
The computed simple interest is :
8100.0

코드 설명

  • 원금, 이자율, 그리고 기간(연 단위)을 사용자 입력으로 받아옵니다. 원금과 이자율은 소수점을 허용하도록 float 타입으로, 기간은 정수형 int 타입으로 변환합니다.

  • 단순 이자를 구하는 공식인 (원금 × 기간 × 이율) / 100을 정의하여 계산을 수행합니다.

  • 계산된 결과는 변수 my_simple_interest에 할당됩니다.

  • 최종적으로 계산된 값이 콘솔 화면에 출력됩니다. 위 예제에서는 원금 45,000원, 기간 3년, 이율 6%가 주어졌을 때 단순 이자 8100.0이 출력되는 것을 확인할 수 있습니다.