0% found this document useful (0 votes)
0 views8 pages

Control Instructions WIth Logical Operators

Uploaded by

are85reh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views8 pages

Control Instructions WIth Logical Operators

Uploaded by

are85reh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Use of Logical Operators # include <stdio.

h>
C allows usage of three logical operators, int main( )
namely, &&, || and !. These are read as ‘AND’,
{
‘OR’ and ‘NOT’ respectively.
float m1, m2, m3, m4, m5 ;
Example 1.1:
float percentage;
The marks obtained by a student in 5 different printf ( "Enter marks in five subjects \n" ) ;
subjects are input through the keyboard. The
scanf ( "%f %f %f %f %f", &m1, &m2, &m3, &m4, &m5 ) ;
student gets a division as percentage the
following rules: percentage = ( m1 + m2 + m3 + m4 + m5 ) / 500 * 100 ;

 Percentage above or equal to 60 - First division if ( percentage >= 60 )

 Percentage between 50 and 59 - Second division printf ( "First division\n %.2f", percentage ) ;
if ( percentage >= 50 && percentage < 60 )
 Percentage between 40 and 49 - Third division
printf ( "Second division\n %.2f", percentage ) ;
 Percentage less than 40 - Fail
if ( percentage >= 40 && ( percentage < 50)
Write a program to calculate the division
printf ( "Third division\n %.2f" , percentage ) ;
obtained by the student.
if ( percentage < 40 )
printf ( "Fail\n %.2f", percentage ) ;
return 0;
}
The problem in the above solution is that even if the first condition turns out to be true, all other conditions are still checked. This will
increase the time of execution of the program.
# include <stdio.h>
int main( )
The else if Clause
{

Example 1.2: float m1, m2, m3, m4, m5 ;

The marks obtained by a student in 5 different float percentage;


subjects are input through the keyboard. The printf ( "Enter marks in five subjects \n" ) ;
student gets a division as percentage the
scanf ( "%f %f %f %f %f", &m1, &m2, &m3, &m4, &m5 ) ;
following rules:
percentage = ( m1 + m2 + m3 + m4 + m5 ) / 500 * 100 ;
 Percentage above or equal to 60 - First division
if ( percentage >= 60 )
 Percentage between 50 and 59 - Second division
printf ( "First division\n %.2f", percentage ) ;
 Percentage between 40 and 49 - Third division
else if ( percentage >=50 )
 Percentage less than 40 - Fail
printf ( "Second division\n %.2f", percentage ) ;
Write a program to calculate the division
obtained by the student. else if ( percentage >=40)
printf ( "Third division\n %.2f" , percentage ) ;
else
printf ( "Fail\n %.2f", percentage ) ;
return 0;
}
Using logical operators

Example 1.3:
A company insures its drivers in the following cases: 1. # include <stdio.h>
2. int main( )
 If the driver is
3. {
 married.
4. char gender, married ;
OR
5. int age ;
 If the driver is
6. printf ( "Enter age, gender (M/F), married (Y/N) \n" ) ;
 unmarried,
7. scanf ( "%d %c %c", &age, &gender, &married ) ;
 male &
8. if ( ( married == 'Y') || ( married == 'N' && gender == 'M' && age
 above 30 years of age. > 30 ) ||
OR ( married == 'N' && gender == 'F' && age > 25 ) )
 If the driver is 10. printf ( "Driver should be insured\n" ) ;
 unmarried, 11. else
 female & 12. printf ( "Driver should not be insured\n" ) ;
 above 25 years of age. 13. return 0 ;
14. }
 You’re creating a program to determine if someone qualifies as a 'Pizza VIP' who
gets access to a secret pizza party. The conditions are strict! To be a true 'Pizza
VIP,' a person must:
 Be at least 18 years old (no kids allowed—more pizza for us!).
 Have eaten more than 10 pizzas in the last month (we’re talking real pizza lovers
here).
 However, if they work at a pizza place, they’re automatically in, regardless of their age
or how much pizza they've eaten!
Example 1.4:
 Write a C program that asks the user for their age, how many pizzas they’ve
eaten in the last month, and if they work at a pizza place ('Y' for yes, 'N' for no).
Use an if statement with logical operators to decide if they qualify as a 'Pizza VIP.'
Solution:
1. #include <stdio.h
2. int main() {
3. int age, pizzasEaten;
4. char worksAtPizzaPlace;
5. printf("Enter your age: ");
6. scanf("%d", &age);
7. printf("How many pizzas have you eaten in the last month? ");
8. scanf("%d", &pizzasEaten);
9. printf("Do you work at a pizza place? (Y/N): ");
10. scanf(" %c", &worksAtPizzaPlace);
11. if ((age >= 18 && pizzasEaten > 10) || (worksAtPizzaPlace == 'Y' || worksAtPizzaPlace == 'y'))
{
12. printf("Welcome, Pizza VIP! Get ready for the ultimate pizza party!\n");
13. } else {
14. printf("Sorry, you’re not quite 'Pizza VIP' material. Maybe eat more pizza next time!\n");
15. }
16. return 0;
17. }
Combination of the if—else if—else and logical operators 6. scanf ( " %c%d%d", &g, &yos, &qual ) ;
7. if ( g == 'm' && yos >= 10 && qual == 1 )
Example 1.5: 8. sal = 11000 ;
Write a program to calculate the salary as percentage the 9. else if ( ( g == 'm' && yos >= 10 && qual == 0 ) ||
following table: ( g == 'm' && yos < 10 && qual == 1 ) )
10. sal = 10000 ;
11. else if ( g == 'm' && yos < 10 && qual == 0 )
12. sal = 7000 ;
13. else if ( g == 'f' && yos >= 10 && qual == 1 )
14. sal = 12000 ;
15. else if ( g == 'f' && yos >= 10 && qual == 0 )
16. sal = 9000 ;

1. int main( ) 17. else if ( g == 'f' && yos < 10 && qual == 1 )
18. sal = 10000 ;
2. {
19. else if ( g == 'f' && yos < 10 && qual == 0 )
3. char g ;
20. sal = 6000 ;
4. int yos, qual, sal = 0 ; 21. printf ( "\nSalary of Employee = %d\n", sal ) ;
5. printf ( "Enter Gender (m/f), Years of Service and 22. return 0 ;
Qualifications (0 = G, 1 = PG): " ) ;
23. }
The ! Operator HIERARCHY OF OPERATORS

int y=8;
if (!(y < 10 ) )
printf ("y is less than 10");
else Operators Type

printf("y greater than 10"); ! Logical NOT

* / % Arithmetic and modulus

+ - Arithmetic

< > <= >= Relational

== != Relational

&& Logical AND

|| Logical OR

= Assignment
The conditional operators ? and : are sometimes called ternary (triple) operators since they take three arguments.

EXPRESSION 1 ? EXPRESSION 2 : EXPRESSION 3


“IF EXPRESSION 1 IS TRUE, THEN THE VALUE RETURNED WILL BE EXPRESSION 2, OTHERWISE THE VALUE RETURNED WILL BE EXPRESSION 3”

Example 1.6 Example 1.7


#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
int x, y; int x;
printf("Enter any number\n"); printf("Enter any number\n");
scanf("%d",&x); scanf("%d",&x);
y = (x>5? 3:4); printf(x==1? "It's Sunday": "It's NOT
Sunday");
printf("value of y = %d", y);
return 0;
return 0;
}
}
This statement will store 3 in y if x is greater than 5,
otherwise it will store 4 in y.

You might also like