3. If-else-if statement

Syntax:-
if(<condition1>)
Statement1;
else if(<condition2>)
statement2;
else if(<condition3>)
statement3;
else
statement4;
If the condition1 is true then statement1 will be executed and the remaining statements will be skipped. If the condition1 is false then it will check the condition2, if it is true then statement2 will be executed and the remaining statements will be skipped. And so.on if all conditions are false then it will execute statement4. Else part is optional. If there is no else part if all the conditions are fail there is no result.
/* Write a program to calculate the monthly telephone bills as per the following rule:
Minimum Rs. 200 for upto 100 calls.
Plus Rs. 0.60 per call for next 50 calls.
Plus Rs. 0.50 per call for next 50 calls.
Plus Rs. 0.40 per call for any call beyond 200 calls. */

#include<stdio.h>
main()
{
int noc;
float bill;
clrscr();
printf("Enter no.of calls:");
scanf("%d",&noc);
if(noc>=0 && noc<=100)
bill=200;
else if(noc<=150)
bill=200+(noc-100)*0.60;
else if(noc<=200)
bill=200+30+(noc-150)*0.50;
else if(noc>200)
bill=200+30+25+(noc-200)*0.40;
printf("\nBill = %.2f",bill);
getch();
}

No comments:

Post a Comment