×

C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

Logical OR (||) operator with example in C language

C language Logical OR (||) operator: Here, we are going to learn about the Logical OR (||) operator in C language with its syntax, example.
Submitted by IncludeHelp, on April 14, 2019

Logical operators work with the test conditions and return the result based on the condition's results, these can also be used to validate multiple conditions together.

In C programming language, there are three logical operators Logical AND (&&), Logical OR (||) and Logician NOT (!).

Logical OR (||) operator in C

Logical OR is denoted by double pipe characters (||), it is used to check the combinations of more than one conditions; it is a binary operator – which requires two operands.

If any of the operand's values is non-zero (true), Logical OR (||) operator returns 1 ("true"), it returns 0 ("false") if all operand's values are 0 (false).

Syntax of Logical OR operator:

    condition1 || condition2

Truth table of logical OR operator:

condition1 	condition2	condition1 || condition2
1		1		1
1		0		1
0		1		1
0		0		0

C examples of Logical OR (||) operator

Example 1: Take a number and apply some of the conditions and print the result of expression containing logical OR operator.

// C program to demonstrate example of 
// Logical OR (||) operator 

#include <stdio.h>

int main()
{
    int num =10;
    
    //printing result with OR (||) operator
    printf("%d\n",(num==10 || num>=5));
    printf("%d\n",(num>=5  || num<=50));
    printf("%d\n",(num!=10 || num>=5));
    printf("%d\n",(num>=20 || num!=10));
    
    return 0;
}

Output

1
1
1
0

Example 2: Input gender in single character and print full gender (Ex: if input is 'M' or 'm' – it should print "Male").

// Input gender in single character and print full gender
// (Ex: if input is 'M' or 'm' - it should print "Male").

#include <stdio.h>

int main()
{
    char gender;
    
    //input gender
    printf("Enter gender (m/M/f/F): ");
    scanf("%c", &gender);
    
    //check the condition and print gender
    if(gender=='m' || gender=='M')
        printf("Gender is Male");
    else if(gender=='f' || gender=='F')
        printf("Gender is Female");
    else
        printf("Unspecified gender");
        
    return 0;
}

Output

Enter gender (m/M/f/F): m
Gender is Male

Second run:
Enter gender (m/M/f/F): F
Gender is Female 

Third run:
Enter gender (m/M/f/F): x
Unspecified gender

Example 3: Input an integer number and check whether it is divisible by 9 or 7.

// Input an integer number and check whether 
// it is divisible by 9 or 7.

#include <stdio.h>

int main()
{
    int num;
    
    //input number
    printf("Enter an integer number: ");
    scanf("%d", &num);
    
    //check the condition
    if(num%9==0 || num%7==0)
        printf("%d is divisible by 9 or 7\n",num);
    else
        printf("%d is not divisible by 9 or 7\n",num);
        
    return 0;
}

Output

First run:
Enter an integer number: 27
27 is divisible by 9 or 7

Second run:
Enter an integer number: 49
49 is divisible by 9 or 7

Third run:
Enter an integer number: 25
25 is not divisible by 9 or 7



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.