Decision Statements 85
OUTPUT:
Enter Three Numbers: 1 2 4
Addition : 7
Multiplication : 8
Enter Three Numbers: 5 v 8
Explanation The variable ' x ' contains the number of values correctly inputted by the user. If the
value of 'x ' is three the addition and multiplication operations are performed as per first example
in the output. In the second example the numbers are not correctly entered. Hence, the i f condition
is false and no operation is performed. Here, the statements are following i f condition is enclosed
within curly braces.
5.3 THE i f ... e ls e STATEMENT
We observed the execution of i f statement in the previous programs. It is observed that the i f
statement executes only when the condition following i f is true. It does nothing when the condition
is false.
The i f .. else statement takes care of true as well as false conditions. It has two blocks. One
block is for i f and it is executed when the condition is true. The other block is of else and it is
executed when the condition is false. The else statement cannot be used without i f . No multiple
e ls e statements are allowed with one i f .
The format of if..else statement is as follows
if(the condition is true)
execute the Statementl;
else
execute the Statement2;
OR
Syntax of if - else statement can be given as follows.
if ( expression is true)
{
statement 1; »
statement 2;
}
if block
else
{
statement 3; »
else block
86 Programming and Data Structures
Statement 4;
}
Demonstration of i f .. e ls e statement is given in the following programs
5J>Head the values of a, b, c through the keyboard. Add them and after addition check if if
is in the range of 100 & 200 or not Print separate message for each.
toainO
{
int &,b,c,d;
c l r s c r () ;
printf ("Enter Three Numbers a b c
scanf ("%d%d%d", &ca,&bM);
printf ("a=%d b=%d c=%d ",a,b,c);
d=a+b+c;
if(d<=200 &d>=100)
printf ("nSum is %d which is in between 100 & 200",d);
else
printf ( Sum is %d which is out of range ",d);
/
OUTPUT : Enler Three Numbers a b c : 50 52 54
A=50 b=52 c=54
Sum is 156 which is in between 100 & 200.
Explanation In the above program three numbers are entered and their sum is assigned to d. The
value of d is checked by the i f statement. If the value of ' d# is greater than 200 and less than 100 i f
block is executed otherwise else block is executed.
5.6 Write a program to find the roots of a quadratic equation by using if else condition.
tin c lu d e <math.h>
main()
{
int b ,a,c;
f l o a t xl,x2;
c lrs c r ();
printf (" Enter Values for a,b,c
scanf r % d %d %d", &a,&b,&c);
if(b*b>4*a*c)
{
xl~-b+sqrt(b *b-4*a *c)!2 *a;
Decision Statements 87
x2=-b->qrtw*b-4*a*c)l2'>a;
printf (" xl=% f x2=% f',xl,x2);
}
else
printf (" Roots are Imaginary");
getchO;
OUTPUT;
Enter Values for a,b,c : 5 1 5
Roots are Imaginary
Explanation The user can enter the values of a, b & c in the above program. The term b2 and
4*a*c are evaluated. The i f condition checks whether b J is greater than 4*a*c. If true, x l andx2
are evaluated and printed otherwise message displayed will be "Roots are Imaginary".
5.7 Write a program to calculate the square of those numbers only wiiose least significant
digit is 5.
# in c lu d e <atdio.h>
# in c lu d e <conio.h>
v o id main()
{
in t s,d;
c lr a c r ();
printf (J/ Enter a Number:");
scanf ("%d",&s);
d=s%10;
if(d==5)
{
s=s/10;
printf ("n Square = %d%d",s*s++,d*d);
I
else
printf (“ Invalid Number");
)
Q m e r n :
Enter a Number : 25
Square = 625
88 Programming and Data Structures
Explanation In the above program a number whose square is to be computed is entered. With
modular division operation last digit is separated to confirm whether it is 5 or not. If yes, the body
of the i f loop is executed where 10 divides the entered number and the quotient is obtained. The
quotient and its consecutive number are multiplied and displayed. Followed by this square of 5 is
calculated and displayed. Care is taken in the printf () statement to display the two results (a)
multiplication of quotient and its consecutive number and b) square of five ) without space. Thus,
the square of a number is displayed.
5.8 Write a program to calculate the salary of medical representative based on the sales.
Bonus and incentive to be offered to him will be based on total sales. If the sale exceeds
Rs.100000/- follow the particulars of Table (1) otherwise (2).
1. TABLE 2. TABLE
Basic= Rs. 3000. Basic= Rs. 3000.
Hra=20% of basic. Hra=20% of basic.
Da= 110% of basic. Da= 110% of basic.
Conveyance=Rs.500. Conveyance=Rs.500.
Incentive=10% of sales. Incentive=5% of sales.
Bonus=Rs. 500. Bonus=Rs. 200.
v o id main()
{
f l o a t b s , h r a , d a, c v , in c e n tiv e , bonus, s a l e , t s ;
clrscr();
printf (" Enter Total Sales in Rs.:");
scanf &Lsale);
if (sale>=100000)
{
bs=3000;
hra=20 * bs/100;
da= 110 * bs/100;
cv=500;
i ttcen tive=sale *20/100;
bonus=500;
I
else
I
bs=3000;
hra=20 4 bs/100;
da= 110 * bs/100;
cv=500;
incetitive=sale*5/100;
bonus-200;
I
ts=bs+hra+da+cv+incentive+bonus;
..................Content has been hidden....................

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