Logical OR operator explanation with example in C

Logical OR operator examples in C programming language :

Logical OR is used with two operands. The definition of logical OR is that it will return true if any of the operands is true. Else, it returns false. In C programming, we don’t have any boolean, so it returns 1 for true and 0 for false.

Two vertical lines || are used to define logical OR operator. In this tutorial, I will write different examples to give you a better understanding of how logical OR works in C.

Example 1: Check even number or divisible by 5 :

This program will take one number as input from the user, check if it is even or divisible by 5 and prints out a message to the user.

#include < stdio.h>

int main()
{
    int no;

    printf("Enter a number that is even or divisible by 5 : ");
    scanf("%d", &no);

    if (no % 2 == 0 || no % 5 == 0)
    {
        printf("Good !!\n");
    }
    else
    {
        printf("This is not a correct input.\n");
    }
}

Sample Output :

Enter a number that is even or divisible by 5 : 13
This is not a correct input.

Enter a number that is even or divisible by 5 : 12
Good !!

Enter a number that is even or divisible by 5 : 5
Good !!

Enter a number that is even or divisible by 5 : 10
Good !!

Integer variable no is used to store the user input. The if condition checks if it is even or divisible by 5. Based on it, one message is printed to the user.

Example 2: Check vowel :

This program will take one character input from the user and prints out if it is a vowel or not.

#include < stdio.h>

int main()
{
    char c;

    printf("Enter a character : ");
    scanf("%c", &c);

    if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U')
    {
        printf("You have entered a vowel\n");
    }
    else
    {
        printf("Entered character is not vowel\n");
    }
}

Sample output :

Enter a character : a
You have entered a vowel

Enter a character : E
You have entered a vowel

Enter a character : v
Entered character is not vowel

c logical vowel check

In this program, we are reading the user input character in variable c. OR(||) is used to check if the input character is a vowel or not. The OR condition checks for both upper and lower case vowels. It prints out a message to the user based on the check.