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

C 언어로 전기 요금 계산 프로그램 만들기

전기 요금은 사용자가 소비한 전력 사용량(단위 수)을 기준으로 산출됩니다. 사용량이 많아질수록 단위당 요금(단가)도 함께 인상되는 누진제 방식이 적용됩니다.

요금 계산 로직

사용자의 최소 사용량이 50단위 미만일 때 적용되는 로직은 다음과 같습니다.

if (units < 50){
   amt = units * 3.50;
   unitcharg = 25;
}

사용량이 50단위 이상 100단위 이하일 때 적용되는 로직은 아래와 같습니다.

else if (units <= 100){
   amt = 130 + ((units - 50 ) * 4.25);
   unitcharg = 35;
}

사용량이 100단위 이상 200단위 이하일 때 적용되는 로직은 다음과 같습니다.

else if (units <= 200){
   amt = 130 + 162.50 + ((units - 100 ) * 5.26);
   unitcharg = 45;
}

사용량이 200단위를 초과할 때 적용되는 로직은 아래와 같습니다.

amt = 130 + 162.50 + 526 + ((units - 200 ) * 7.75);
unitcharg = 55;

각 구간에서 계산된 기본 요금(amt)과 단위 요금(unitcharg)을 더해 최종 청구 금액이 산출됩니다.

total = amt + unitcharg;

예제 코드

다음은 전기 요금을 계산하는 C 프로그램의 전체 코드입니다.

#include <stdio.h>
int main(){
   int units;
   float amt, unitcharg, total;
   printf(" Enter no of units consumed : ");
   scanf("%d", &units);
   if (units < 50){
      amt = units * 3.50;
      unitcharg = 25;
   }else if (units <= 100){
      amt = 130 + ((units - 50 ) * 4.25);
      unitcharg = 35;
   }else if (units <= 200){
      amt = 130 + 162.50 + ((units - 100 ) * 5.26);
      unitcharg = 45;
   }else{
      amt = 130 + 162.50 + 526 + ((units - 200 ) * 7.75);
      unitcharg = 55;
   }
   total = amt + unitcharg;
   printf("electricity bill = %.2f", total);
   return 0;
}

실행 결과

위 프로그램을 실행하면 다음과 같은 결과를 확인할 수 있습니다.

Enter no of units consumed: 280
electricity bill = 1493.50

이 예제에서는 사용량이 280단위이므로 200단위 초과 구간이 적용됩니다. 기본 요금 130원과 162.50원, 그리고 526원이 합산된 뒤, 초과분인 80단위에 단가 7.75원이 곱해지고 여기에 단위 요금 55원이 추가되어 총 1,493.50원이 청구됩니다.