소개
이 랩에서는 주어진 원금, 이자율 및 기간에 대한 단순 이자를 계산하는 C 프로그램을 작성하는 방법을 배우게 됩니다.
변수 초기화
principal_amt, rate, simple_interest, 및 time 변수를 각각 float 및 integer 타입으로 선언하고 초기화합니다. 아래와 같습니다.
#include <stdio.h>
int main()
{
float principal_amt, rate, simple_interest;
int time;
}
사용자 입력 받기
scanf를 사용하여 사용자로부터 원금, 이자율 (백분율) 및 기간 (년) 을 입력받습니다.
printf("Enter the value of principal amount, rate and time\n\n\n");
scanf("%f%f%d", &principal_amt, &rate, &time);
float 타입 입력을 읽기 위해 %f 형식 지정자를 사용하고, integer 타입 입력을 읽기 위해 %d를 사용합니다.
단순 이자 계산
다음 공식을 사용하여 단순 이자를 계산합니다.
Simple Interest = (principal_amt * rate * time) / 100
아래 코드를 사용하여 단순 이자를 계산합니다.
simple_interest = (principal_amt*rate*time)/100.0;
결과 출력
printf 함수를 사용하여 출력 텍스트와 변수를 콘솔에 출력합니다.
printf("\n\n\t\t\tAmount = Rs.%7.3f\n ", principal_amt);
printf("\n\n\t\t\tRate = Rs.%7.3f\n ", rate);
printf("\n\n\t\t\tTime = %d years \n", time);
printf("\n\n\t\t\tSimple Interest = Rs.%7.3f\n ", simple_interest);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
소수점 이하 3 자리를 포함하여 총 7 자리로 출력을 형식화하려면 %7.3f를 사용합니다.
전체 코드 작성
1-4 단계의 코드를 아래와 같이 ~/project/main.c 파일의 main 함수에 복사합니다.
#include <stdio.h>
int main()
{
printf("\n\n\t\tLabEx - Best place to learn\n\n\n");
float principal_amt, rate, simple_interest;
int time;
printf("Enter the value of principal amount, rate, and time\n\n\n");
scanf("%f%f%d", &principal_amt, &rate, &time);
// considering rate is in percentage
simple_interest = (principal_amt*rate*time)/100.0;
// usually used to align text in form of columns in table
printf("\n\n\t\t\tAmount = Rs.%7.3f\n ", principal_amt);
printf("\n\n\t\t\tRate = Rs.%7.3f\n ", rate);
printf("\n\n\t\t\tTime= %d years \n", time);
printf("\n\n\t\t\tSimple Interest = Rs.%7.3f\n ", simple_interest);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
컴파일 및 실행
C 컴파일러를 사용하여 프로그램을 컴파일하고 실행합니다. 출력은 원금, 이자율 및 기간에 대한 사용자 입력을 요청한 다음 계산된 단순 이자를 표시합니다.
요약
이 랩에서는 변수, 사용자 입력 및 단순 이자 공식을 사용하여 단순 이자를 계산하는 C 프로그램을 작성하는 방법을 배웠습니다. 변수 선언 및 초기화, scanf를 사용한 사용자 입력 받기, printf를 사용한 출력 표시와 같은 기본적인 개념을 다루었습니다. 랩 완료를 축하합니다!



