Pages

This blog is under construction

Sunday, January 6, 2019

Decision-making statements (if-else if-else)Example:

Program:
An electricity board charges the following rates for the use of electricity:
For the first 200 units; 80 P per unit
For the next 100 units; 90 P per unit
Beyond 300 units; Rs. 1 per unit
All users are charged a minimum of Rs. 100 as meter charge. If the total amount is more than Rs. 400, then an additional surcharge of 15% of total amount is charged.

Write a program to read the names of users and number of units consumed and printout the charges with names.

C Code:

#include<stdio.h>
void main()
{
  int Units;
char Name[10];
float Charge;  
    
printf("Enter Name of User:--\n");
scanf("%s",Name);
printf("Enter Total Units Consumed\n");
scanf("%d",&Units);
if(Units>=0&&Units<=200)
Charge=100+(Units*0.80);
else if(Units>200&&Units<=300)
Charge=100+(Units*0.90);
else if(Units>300&&Units<=400)
Charge=100+Units;
else
Charge=(100+Units)+(100+Units)*15;
printf("Name Units Charge\n");
printf("%s %d %.2f",Name,Units,Charge);
}

Output:

Enter Name of User:--  Amit                                                                                                                       
Enter Total Units Consumed  34                                                          
Name   Units    Charge                                                                
Amit      34        127.20                                                                                                                
               

No comments:

Post a Comment