Decision Statements 89
printf ("rtTotal Sales : %.2f,sale);
printf (" iBasic Salary: %.2f',bs);
printf (" Hra : %.2f',hra);
printf ("nDa : %.2f',da);
printf ("nConveyance : %.2f',cv);
printf ("nlncentive : %.2f',incentive);
printf ("nBonus ; %.2f'/bonus);
printf ("nGross Salary : %.2f',ts);
getchO;
i
OUTPUT : Enter Total Sales in Rs.: 100000
Total Sales
Basic Salary
Hra
Da
Conveyance
Incentive
Bonus
Gross Salary
100000.00
3000.00
600.00
3300.00
500.00
10000.00
500.00
17900.00
Explanation This program calculates the salary of a medical representative depending on sales.
H>e Basic salary is same but other allowances and incentive changes depends on sales. If sale is
more than 100000 rate of allowances and incentive are as per table (1) otherwise table (2). The i f
condition checks the given figure of sales. If sale is larger than Rs.100000 first block following i f
statement is executed otherwise
else block is executed. In both the blocks simple arithmetic
operations are performed to calculate the allowances and total salary.
5.4 NESTED i f -else STATEMENT
In this kind of statements number of logical conditions are checked for executing various statements.
Here, if any logical condition is true the compiler executes the block followed by i f condition
otherwise it skips and executes else block. In i f . . else statement else block is executed by
default after failure of condition. In order to execute the e ls e block depending upon certain condition
we can add repetitively i f statements in else block. This kind of nesting will be unlimited.
The syntax of the i f . . e ls e . . i f ladder is as follows.
Syntax of if - else ..if statement can be given as follows.
if ( condition)
statement 1;
statement 2;
if block
90 Programming and Data Structures
else if (condition)
I
statement 3; -» else block
statement 4;
}
else
{
statement 5;
statement 6;
)
From the above block following rules can be described for applying nested i f .e l s e .i f statements.
1. Nested i f .. e 1 s e can be chained with one another.
2. If the condition is false control passes to else block where condition is again checked with the
if statement. This process continues if there is no if statement in the last else block.
3. If one of the if statement satisfies the condition, other nested else. . i f will not be executed.
The programs given below are based on nested i f . . e l s e . . i f statements.
5.9 Write a program to calculate energy bill. Read the starting and ending meter readings
The charges are as follows.
No. Of Units Consumed Rates In (Rs.)
200 - 500 3.50
100 - 200 150
Lt->s than 100 1.50
mainO
{
int initial,final,conauaed;
float total;
c lr s c r ();
printf (" Initial & Final Reading,t f ) ;
scanf ("\%d %d", fdnitial, Stfinal);
consutned * final-initial;
if (consutned>*200 && consutned<m500)
to talaconsunud * 3-50;
else if (consumed>al00U tccansumed<ml99)
total« consutned *2.501?
elseif(ccH»umed<100)
total~consumed*lJ50;
printf ("To tal bill for %d unit is %f*£Onsumed,total);
getcheO;
Decision Statements 91
OUTPUT ; Initial & Final Readings : 800 850
Total bill for 50 unit is 75.000000
Explanation Initial and final readings are entered through the keyboard. Their difference is the
total energy consumed. As per the table rates are applied and total bill based on consumption of
energy is calculated.
5.10 Write a program to calculate energy bill. Read the starting and ending meter reading. If
the consumed electricity energy is greater than or equal to 200 units the rate should be
2.50 / unit otherwise 1.50 / unit.
mainO
{
in t in iti a l,fin a l,c o n su m e d ;
f l o a t t o t a l ;
clrscr();
printf (" Initial & Final Readings
scanf ("\%d %d", &initial, &final);
consumed = final-initial;
if (consumed>=200)
total-consumed *2.50;
else
total= consumed * 1.50;
printf ("Total bill for %d unit is %f',consumed,total);
getcheO;
i
OUTPUT : Initial & Final Readings : 900 800
Total biU for 100 unit is 150.000000
Explanation The logic of the program is same as explained in the previous example. Instead of
nested if' s only if e ls e condition is used.
5.11 Write a program to sort six numbers and find die largest one by using ladder of if else.
# in clu d e< std io . h>
mainO
{
in t a,b,c,d,e,f;
clrscr();
printf(Enter 1st number:");
scanf(" %d",&a);
92 Programming and Data Structures
printf("Enter 2nd number/');
scanfCn%d",&cb);
printf("Enter 3 rd number:");
$canf(" %d",&Lc);
printf(“Enter 4th number:");
$canf(" %d",&d);
printf("Enter 5th number:");
scanf("n%d",&ce);
printf("Enter 6th number:");
8canf(" %d",&f);
if((a>b) && (a>c) && (a>d) && (a>e) && (a>f))
printf(“Highest of six Number is : %d",a);
else if (fb>a) && (b>c) && (b>d) && (b>e) && (b>f))
printf("Highest of six Number is: %d",b);
eke if ((o a ) && (o b ) && (o d ) && (oe) && (c>/))
printf ("Highest of six Number is: %d",c);
else if ((d>a) && (d>b) && (d>c) && (d>e) && (d>f))
printf(J/Highest of six Number is : %d",d);
else if ((e>a) && (e>W && && (e>d) &&
printf("Highest of six Number is : %d",e);
else
printf("Highest of Six Number is : %d",f);
getchO;
}
Q in ru T :
Enter 1st number: 52
Enter 2nd number 74
Enter 3rd number 90
Enter 4th number 45
Enter 5th number 10
Enter 6th number 22
Highest of Six Number is : 90
Explanation Six numbers are entered through the keyboard. All the values of the variables are
compared with one another using && (and) in nested i f . . e ls e .. i 1 statements. When one of the
i f statement satisfies the condition that i f block is executed which prints the largest number
otherwise the control passes to another i f . .else, . i f statement.
5.12 Write a program to find largest number out of three numbers. Read the numbers through
the keyboard.
mainO
I
Decision Statements 93
in t x,y,z;
c l r s c r ( ) ;
printf (" Enter Three Numbers x,y,z
scauf C%d %d %d", 6cx,8cy,Scz);
printf (" Largest out of Three Numbers is
if(x>y)
I
if (x>z)
printf ("x=%d "jc);
else
printf ("z=%dn",z);
I
else
{
if(z>y)
printf ("z=%d ",z);
else
printf ("y=%d ",y);
}
OUTPUT : Enter Three Numbers x, y, z : 10 20 30
Largest out of Three Numbers is .z=30
Explanation This is also an example of nested i f s. When the i f statement satisfies the condition
control passes to another i f statement block. Three numbers are entered through the keyboard. The
first i f statement compares first number with the second number. If the condition is true block
following first i f s ta tem en t executes. Inside the block i f statement checks whether first number
is larger than the third number. If yes then, the largest number is the first one and the same is
displayed. Else the third number is largest and the same is printed.
In case the first i f statement fails to satisfy the condition else block with nested i f would be
executed. The third number is compared with the second number. If it is true then third number is
the largest otherwise second number is the largest.
5.13 Write a program to find the smallest out of the three numbers.
# in clu d e < std io .h >
mainO
{ i n t a ,b ,c ,s m a l l e s t ;
c l r s c r ( ) ;
printf (n Enter Three Numbers
scanf ("%d %d %d", &a,&b,&c);
if(a<b)
f
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.93.132