# Logical Operators in C with Examples

**Program Abstraction:**

*This C programming tutorial describes the concept of Logical Operators with examples*

Logical operators are the operators using for logical operations. They are 3 types:

**&& (Logical AND)****|| (Logical OR)****! (Logical NOT)**

**1. && (Logical AND)**

**
** In C, this operator using instead of the word ‘

**and**’ in English. Let us see an example:

*‘If I am a writer*

**and**you are a reader then let us sign a song’.*Here we will sing if and only if I am a writer and you are reader, otherwise not. Here the*

**and**denotes the logical AND.

**Let us see an example in C: ***A program than accept two numbers. If first number is smaller than 10 and second number is larger than 10, then display ‘Thanks’*

#include<stdio.h> #include<conio.h> void main() { int a, b; clrscr();
printf(“Enter the first number ”);
scanf(“%d”,&a); printf(“nEnter the second number ”); scanf(“%d”,&b); if(a<10 && b>10) { printf(“Thanks”); } getch(); } |

**(a<10 && b>10) **

**a**(first number) is less than 10

*and*

**b**(second number) is greater than 10, then the program will display ‘Thanks’. That is, if the both condition is true, then program will display ‘Thanks’, If any one of condition is true, then program will not display ‘Thanks’.

**Sample Output:**

**Another Example:*** If both two numbers are greater than 10, then display the sum of two numbers*

#include<stdio.h> #include<conio.h>
void main() { int a, b; printf(“Enter the first number”); scanf(“%d”,&a); printf(“nEnter the second number”); scanf(“%d”,&b); if( (a && b) > 10) { printf(“%d”, a+b); } getch(); } |

__Sample Inputs and Outputs__

a | b | Output |
---|---|---|

5 | 15 | Thanks |

7 | 9 | no output |

12 | 23 | no output |

10 | 10 | no output |

-11 | 500 | Thanks |

**2. || (Logical OR) **

Implementing of Logical OR is same as Logical AND, but working is not same. Here if anyone of conditions is true, then program block will work. No need to satisfy both condition like Logical AND.

**Example:** If I am a driver or you are a driver, then let us go to the Park. Here, no need to both are driver to go to the Park.

**Example with C:** *Accept two number, if first number is less than 25 or second number is greater than 25, then display their sum.*

#include<stdio.h> #include<conio.h> void main() { int a, b; printf(“Enter the first number”); scanf(“%d”,&a); printf(“nEnter the second number”); scanf(“%d”,&b); if(a <25 || b > 25) { printf(“%d”, a+b); } getch(); } |

Here no need to satisfy both condition to display their sum.

**Sample Output:**

__Sample Inputs and Outputs__

a | b | Output |
---|---|---|

20 | 30 | 50 |

10 | 23 | 33 |

28 | 52 | 80 |

25 | 25 | no output |

35 | 14 | no output |

**3. ! (Logical NOT) **

* Not* is the just complimentary of any condition or statement.

**Example**: if he is not a singer, then I will sing a song.

Here if he is a singer, then I can’t sing. So if I have to sing a song, he should not be a singer.

**Example in C: **

*Accept two numbers, if the sum of them is not greater than 20, then display their sum*

#include<stdio.h> #include<conio.h> void main() { int a, b; printf(“Enter two numbers”); scanf(“%d%d”,&a,&b); if (!((a+b)>20)) { printf(“%d”, a+b); } getch(); } |

Sum will display if and only if their sum is less than 20.

__Sample Output:__

__Sample Inputs and Outputs__

a | b | Output |
---|---|---|

8 | 8 | 16 |

10 | 9 | 19 |

1 | 16 | 17 |

10 | 10 | no output |

15 | 15 | no output |

## Be First to Comment