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.
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 (" Condition : Return Valuesn" );
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
52 Programming and Data Structures
clrscr();
printf ("tiEntera Character:");
scanf (“%c”, Sac);
y=(x>=65 & & x<=90 ? 1 : 0);
printf (“Y :%d”,y);
I
OUTPUT : Enter a Character: A
Y: 1
Enter a Character : a
Y: 0
Explanation In the above program a character is entered. Using logical operator AND entered
characters ASCII value is checked .If it is in between 65 and 90 the result displayed will be 1 otherwise
0. The AND operator joins two conditions. If the condition is true 1 is assigned to y otherwise 0.
3.13 Write a program to display logic 0 if one reads a character through keyboard otherwise
1. (ASCII values for 0 to 9 are 48 to 57 respectively.)
mainO
{
in t yi
ch ar x j
c l r s c r ( ) ;
printf (" Enter The Character or Integer:);
scanf (M %c", Sex);
y=(x>=48 && x<=57 ? 1 0);
Printf (" Value of Y =%d",y);
I
OUTPUT : Enter The Character or Positive Integer : A
Value of Y = 0
OR
Enter The Character or Positive Integer : 1
Value of Y = 1
Explanation The above program is same as the previous one. Here, ASCII range 48 to 57 is used.
Equivalent of these values are 0 to 9 digits respectively. If the entered number is in between 0 to 9 the
compiler returns 1 otherwise 0.
Operators and Expressions 53
3.14 Write a program to display 1 if inputted number is between 1-100 otherwise 0. Use the
logical AND (&&) operator.
mainO
{
in t x,z;
c l r s c r () ;
printf (“ Enter numbers
scanf ("%d", &x);
z=(x>=l & & x<=100 ? 1 : 0);
printf rz=% d", z);
I
OUTPUT : Enter numbers : 5
Z = 1
Enter numbers : 101
Z = 0
Explanation The logical operator checks the entered value whether it is in between 1 and 100. If
the condition is true, 1 is assignied to z otherwise 0.
3.15 Write a program to display 1 if inputed number is either 1 or 100 otherwise 0. Use the
logical OR (II) operator.
main()
{
in t x,z;
c l r s c r ( ) ;
printf (“ Enter numbers
scanf ("%d", tkx);
z=(x=—l I I x==100 ? 1 : 0);
printf (“Z~%d", z);
I
OUTPUT : Enter numbers : 1
Z = 1
Enter numbers : 100
Z = 1
Enter numbers : 101
Z = 0
Explanation In the above program the OR operator checks two conditions. If one of the conditions
satisfies 1 is assigned to z otherwise 0.
..................Content has been hidden....................

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