How to write a C program to calculate the total electricity bill according to the given conditions using switch cases.
below. (User input : Number of consumed units)
1 – 50 units Rs. 20.00
51-150 units Rs. 35.00
151- 250 units Rs. 45.00
Above 250 Rs. 60.00
An additional surcharge of 15% is added to the bill.
Switch Cases
Share
My Answer
#include
#include
int comparison(int);
int main()
{
int userInputOfUnit;
printf(“Enter The Units: “);
scanf(“%d”, &userInputOfUnit);
float ADDITIONAL = (float) 15/100;
float unitsValue;
float unitPayment;
int res = comparison(userInputOfUnit);
switch (res)
{
case (0):
perror(“innorrect units”);
break;
case(1):
unitsValue = userInputOfUnit * 20;
unitPayment = unitsValue * ADDITIONAL;
printf(“_______________________________________\n\n”);
printf(“\tUnits: %d\n”, userInputOfUnit);
printf(“\tUnits Payment: %.2f\n”, unitsValue);
printf(“\tTotal Payment: %.2f\n”, unitPayment);
printf(“_______________________________________\n”);
break;
case (2):
unitsValue = userInputOfUnit * 35;
unitPayment = unitsValue * ADDITIONAL;
printf(“_______________________________________\n\n”);
printf(“\tUnits: %d\n”, userInputOfUnit);
printf(“\tUnits Payment: %.2f\n”, unitsValue);
printf(“\tTotal Payment: %.2f\n”, unitPayment);
printf(“_______________________________________\n”);
break;
case (3):
unitsValue = userInputOfUnit * 45;
unitPayment = unitsValue * ADDITIONAL;
printf(“_______________________________________\n\n”);
printf(“\tUnits: %d\n”, userInputOfUnit);
printf(“\tUnits Payment: %.2f\n”, unitsValue);
printf(“\tTotal Payment: %.2f\n”, unitPayment);
printf(“_______________________________________\n”);
break;
case (4):
unitsValue = userInputOfUnit * 60;
unitPayment = unitsValue * ADDITIONAL;
printf(“_______________________________________\n\n”);
printf(“\tUnits: %d\n”, userInputOfUnit);
printf(“\tUnits Payment: %.2f\n”, unitsValue);
printf(“\tTotal Payment: %.2f\n”, unitPayment);
printf(“_______________________________________\n”);
break;
default:
perror(“innorrect units”);
}
return 0;
}
int comparison(int value)
{
if (1 <= value && value <= 50)
return 1;
else if (51 <= value && value <= 150)
return 2;
else if (151 <= value && value <= 250)
return 3;
else if (250 <= value)
return 4;
else
return -1;
}
use:
#include
#include
Can I know the meaning of int comparison(int value)?
int comparison is function. int value is argument. you can use this function wherever your program.
value ?
argument name
you can use a switch as followa
switch(payable){
case <50:
//your code; break;
// and so on
}