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 ("\n Calculated value of a is % d , a);
b=(y<=8 ? 10 : 9);
printf ("\n Calculated value ofb is%d", b);
c=(z==10 ? 20 : 30);
printf ("\n Calculated value of c is%d", c);
m=x+y+z;
n=a+b+c;
printf ("\nAddition ofx,y,z is %d (m)", m);
printf (tJ\nAddition of a,b,c is %d (n)"f n);
printf ("\n%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.
Operators and Expressions 51
Table 3.5 Logical Operators
Operator Description or Action Example
Return Value
&&
Logical AND
5>3 && 5<10 1
II
Logical OR 8>5 | | 8<2
1
I Logical NOT
81=8 0
From the above table following rules can be followed for logical operators.
1) The logical AND (&& ) operator provides true result when both expressions are true otherwise 0.
2) The logical OR ( | |) operator provides true result when one of the expressions is true otherwise
0.
3) The logical NOT operator ( l ) provides 0 if the condition is true otherwise 1.
3.11 Example to illustrate the use of logical operators.
# in clu de < atd io.h >
# inclu d e <conio.h>
void mainO
{
c l r s c r ( ) ;
printf ("\nCondition : Return Values\n" );
printf ("\n5>3 && 5<10 : %5d”£>3 && 5<10);
printf ("\n 8>5 II 8<2 : %5d",8>5 I I 8<2);
printf C'\n !(8==8) : %5d",!(8==8));
}
QUJEUI;
Condition : Return Values
5>3 && 5<10 : 1
8>5 I I 8<2 : 0
!(8==8) : 0
Explanation In the above example the first condition is true i.e. 5 is greater than 3 and smaller
than 10. Hence the program returns 1. The second and third conditions are false. Hence result returned
will be 0.
3.12 Write a program to print logic 1 if input character is capital otherwise 0.
m ain()
{
char x ;
in t y t

Get Programming and Data Structures now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.