50 Programming and Data Structures
The following program illustrates the use of various relational operators.
3.10 Write a program to read three variables x, y and z. Use conditional statements and
evaluate values of variables a, b and c. Perform the sum with two sets of variables.
Checks the sums for equality and prints different messages.
mainO
{i n t x , y ,z ,a ,b ,c ,m ,n ;
c l r s c r () ;
p r in tf ("E n ter Values o f x , y , z m) ;
scanf ("%d %d %d*, &x,&y,&z);
a*(x> *5 ? 3 : 4) ;
printf ("
Calculated value of a is % d ”, a);
b=(y<=8 ? 10 : 9);
printf ("
Calculated value ofb is%d", b);
c=(z==10 ? 20 : 30);
printf ("
Calculated value of c is%d", c);
m=x+y+z;
n=a+b+c;
printf ("
Addition ofx,y,z is %d (m)", m);
printf (tJ
Addition of a,b,c is %d (n)"f n);
printf ("
%s”, tn!=n
?"m&in NOT EQUAL" ; "m & n ARE EQUAL ');
t
OUTPUT ; Enter Values of x, y, z 5 2 7
Calculated value of a is 3
Calculated value of b is 10
Calculated value of c is 30
Addition of x,y,z is 14 (m)
Addition of a,b,c is 43 (n)
m & n NOT EQUAL
Explanation In the above program three integers are entered through the keyboard (x , y and
z ). Using conditional statements values of a , b and c are obtained. Sum of x # y , and z is stored
in 'm' and sum of a , b and c is stored in ln '. The variable xm' and 'n ' are compared and
appropriate messages are displayed.
3.6 LOGICAL OPERATORS
The logical relationship between the two expressions are checked with logical operators. Using these
operators two expressions can be joined. After checking the conditions it provides logical tru e (1)
or f a ls e (0) status. The operands could be constants, variables, and expressions. The Table 3.5
decribes the three logical operators together with examples and their return values.