c program for electricty bill calculation


Switch case in c



Switch case can be used for condition checking. The switch case takes a variable as input and in each case, the value stored in the variable is checked for a certain condition. If the condition matches, the statements associated with that case will be executed.


Explanation of the program



This program uses switch case for electricity bill calculation. The program reads the value for current reading and previous reading and stores it in the variables cr and pr respectively. Then the unit is calculated by using the statement unit=cr-pr. Each case checks the value of unit and calculate the corresponding amount.


Program



#include<stdio.h>

#include<conio.h>

void main()

{

int cr,pr,unit,ud,amt;

clrscr();

printf("Enter the current reading\n& previous reading: ");

scanf("%d%d",&cr,&pr);

unit=cr-pr;

ud=unit/100;

switch(ud)

{

case 1: amt=unit+(unit*0.5);

break;

case 2: amt=unit+(unit*1);

break;

case 3: amt=unit+(unit*1.5);

break;

default: amt=unit+(unit*2);

break;

}

printf("Total amount is %d",amt);

getch();

}



Output



c program for electricity bill calculation using switch
C program to calculate electricity bill using switch

No comments: