C Program To Determine Leap Year or Not using Logical Operators

Any year is entered through the keyboard, write a C program to determine whether the year is a leap year or not. Use the logical operators && and ||.

Related Read:
if else statement in C
Relational Operators In C
Logical Operators In C

Other Leap Year C Programs

C Program To Check Leap Year
C Program To Check Leap Year Using Ternary Operator

Leap Year Logic

1. If a year is a century year(year ending with 00) and if it’s perfectly divisible by 400, then it’s a leap year.
2. If the given year is not a century year and it’s perfectly divisible by 4, then it’s a leap year.

Century year is determined by modulo division of the entered year by 100. Example: year%100 == 0. If its century year, it must also be perfectly divisible by 400. i.e., year%400 == 0
(year%100 == 0 && year%400 == 0)

If the year is not century year, then it must be perfectly divisible by 4, for the year to be a leap year.
(year%100 != 0 && year%4 == 0)

Expected Output for the Input

User Input:
Enter a year
2024

Output:
2024 is a leap year

Video Tutorial: C Program To Determine Leap Year or Not using Logical Operators


[youtube https://www.youtube.com/watch?v=8IQcK85dNrw]

YouTube Link: https://www.youtube.com/watch?v=8IQcK85dNrw [Watch the Video In Full Screen.]

Source Code: C Program To Determine Leap Year or Not using Logical Operators

#include < stdio.h >

int main()
{
    int year;

    printf("Enter a year\n");
    scanf("%d", &year);

    if( (year%100 == 0 && year%400 == 0) ||
        (year%100 != 0 && year%4   == 0)   )
    {
        printf("%d is a leap year\n", year);
    }
    else
    {
        printf("%d is not a leap year\n", year);
    }

    return 0;
}

Output 1:
Enter a year
2020
2020 is a leap year

Output 2:
Enter a year
2021
2021 is not a leap year

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Programming Interview / Viva Q&A: 3 (Logical Operator)

In this C program we’ve 2 operands and one AND operator(&&). We use Logical NOT Operator to change the true and false conditions inside if statement. Check the source code below and guess the output.

Related Read:
Logical Operators In C

Logical && Operator in C Program

 
#include < stdio.h >

int main()
{
    int a = 20, b = 30;

    if( a && b )
    {
        printf("a = %d\n", a);
    }
    else
    {
        printf("b = %d\n", b);
    }
    return 0;
}

Output:
a = 20

In above C program, a = 20 and b = 30. In C, any non-zero number is considered as true. So the condition is if statement returns true and the block of code inside if statement gets executed.

Whats the output of this C Program?

 
#include < stdio.h >

int main()
{
    int a = 20, b = 30;

    if(  !(!a) && a  )
    {
        printf("a = %d\n", a);
    }
    else
    {
        printf("b = %d\n", b);
    }
    return 0;
}

Lets evaluate the if condition in above c source code. a = 20, which is non-zero number, so its considered as true(1). So negating the true(1) value !a gives false(0). Negating that false(0) value !(!a) gives true(1).

So !(!a) returns true(1). So both sides of &&(AND) is true, so the whole thing returns true. So the block of code inside if condition gets executed.

Output:
a = 20

C Programming Interview / Viva Q&A: 3 (Logical Operator)


[youtube https://www.youtube.com/watch?v=JxPB_JxNyHY]

YouTube Link: https://www.youtube.com/watch?v=JxPB_JxNyHY [Watch the Video In Full Screen.]


Important:
1. Logical &&(AND) Operator returns true when both sides operands have true(non-zero number) value. In any other case it returns false(0) value.
2. Any non-zero number is considered as true and zero(0) is considered as false in C programming language.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Logical Operators In C

Logical Operators in C programming language return true(non-zero number) or false(0) value. Logical AND(&&) and logical OR(||) works on 2 operands. But logical NOT(!) works on single operand.

Related Read:
Relational Operators In C

Logical Operators

&& – Logical AND Operator.
|| – Logical OR Operator.
! – Logical NOT Operator.

&& – Logical AND Operator.

 
#include < stdio.h >

int main()
{
    int a;

    a = ( 1 && 1 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 1

 
#include < stdio.h >

int main()
{
    int a;

    a = ( 0 && 1 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 0

For logical AND(&&) both operands or expressions must yield to true. If any one condition is false(0), then it’ll return false(0).

|| – Logical OR Operator.

 
#include < stdio.h >
int main()
{
    int a;

    a = ( 1 || 1 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 1

 
#include < stdio.h >
int main()
{
    int a;

    a = ( 1 || 0 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 1

 
#include < stdio.h >
int main()
{
    int a;

    a = ( 0 || 0 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 0

Logical OR(||) returns true(any non-zero number) if either one condition/operand is true. It returns false(0) only when both the conditions / operands are false(0).

! – Logical NOT Operator.

 
#include < stdio.h >
int main()
{
    int a;

    a = ( !1 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 0

 
#include < stdio.h >
int main()
{
    int a;

    a = ( !0 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 1

Logical NOT(!) returns true if the condition is false. It returns false if the condition is true. It just negates the Boolean value given to it.

Logical Operators In C


[youtube https://www.youtube.com/watch?v=9VNNbMEzldw]

YouTube Link: https://www.youtube.com/watch?v=9VNNbMEzldw [Watch the Video In Full Screen.]


Logical Operators combined with Relational Operators

&& – Logical AND Operator.

 
#include < stdio.h >

int main()
{
    int a, b = 100;

    a = ( (b == 0) && (b > 50) );

    printf("Value of a is %d\n", a);

    return 0;
}

Output:
value of a is 1

a is true because both b is equal to 100 is true and b is greater than 50 is true.

 
#include < stdio.h >

int main()
{
    int a, b = 100;

    a = ( (b == 0) && (b > 150) );

    printf("Value of a is %d\n", a);

    return 0;
}

Output:
value of a is 0

a is false(0) because b is equal to 100 is true but b is greater than 150 is false.

|| – Logical OR Operator.

 
#include < stdio.h >

int main()
{
    int a, b = 100;

    a = ( (b == 0) || (b > 50) );

    printf("%d\n", a);

    return 0;
}

Output:
value of a is 1

Value of a is true, because b is equal to true. In logical OR(||) if one condition is true, then it returns true. It returns false(0) only when both the conditions / operands are false(0).

! – Logical NOT Operator.

 
#include < stdio.h >

int main()
{
    int a, b = 100;

    a = ( !(b == 0) );

    printf("%d\n", a);

    return 0;
}

Output:
value of a is 0

Value of a is false(0). Because b is equal to 100 is true. When true value is given to NOT(!) it’ll return false(0). When false value is supplied to NOT it’ll return true.

Note: = is assignment operator. == is equality operator.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Conditional Logic For Decision Making: jQuery

In this video tutorial we shall see how to make our applications smarter with artificial intelligence using conditional logic for decision making, using jQuery.

Here we take 2 numbers from the user and using jQuery, we find the result of division of those two numbers. If the user tries to divide by zero, then we alert the user with a message, that “Division by zero is not allowed”. Thus making our application smarter and get the feel of artificial intelligence!

HTML code
index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<html>
<head><title>Conditional Logic For Decision Making: jQuery</title></head>
<body>
 
<form>
No1: <input type="text" id="no1" /><br />
No2: <input type="text" id="no2" />
<button>Division</button>
</form>
 
<span></span>
 
<script src="script/jquery-1.8.1.min.js" type="text/javascript"></script>
<script src="script/my_script.js" type="text/javascript"></script>
</body>
</html>

Here we have a form, with 2 input box and a button.
There is a span tag to display the appropriate messages.

jQuery code
my_script.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$(document).ready( function() {
 
 $("button").click( function() {
     var no1 = $("#no1").val();
 var no2 = $("#no2").val();
 
 if( no2 == 0 )
   $("span").html("Division by zero not allowed!");
 else if( no2 == 1 )
   $("span").html("Division of "+no1+" and "+no2+" is "+(no1/no2));
 else
 
 $("form").submit( function() { return false; });
 });
});

Here we fetch and assign the values entered by the user to local variables.
using conditional operator, if else, we see if the second number entered by the user is zero.
If its zero, we show “Division by zero not allowed!” message, else we show the result of division.

We also need to make the form submit return false, so that the form doesn’t try to redirect to some other page once the user clicks on the submit button.

Video Tutorial: Conditional Logic For Decision Making: jQuery


[youtube https://www.youtube.com/watch?v=JMXbyAUMmEk]

YouTube Link: https://www.youtube.com/watch?v=JMXbyAUMmEk [Watch the Video In Full Screen.]



Comparison Operators
1. Equality: a == b;
2. Inequality: a != b;
3. Exactly Equal: a === b;
4. Greater than: a > b;
5. Greater than or equal to: a >= b;
6. Less than: a < b; 7. Less than or equal to: a < = b; Logical Operators: Negation: !a OR: a || b; AND: a && b;

TruthTable

Negation: True if a is false or doesn’t exist in the DOM.
OR: True if a is true or b is true, or if both are true, but not if both are false.
AND: True if a is true and b is true, but not if one or the other is false.

Ternary Operator
Ex: a > b ? if_true_code : else_this_code;

If a is greater than b, then if_true_code will be executed, else else_this_code will be executed.

Also see Ternary Operator in C for understanding the logic.
Biggest of Two Numbers Using Ternary Operator: C++.
Biggest of 3 Numbers Using Ternary Operator: C++.

Note: If there is only 1 statement inside the if and/or elseif and/or else, we need not put the code inside the brackets. If there are multiple statements then we MUST put the code inside the brackets. Also we can have any number of elseif’s in a if else block, but only 1 if and only 1 else part in it. We could have any number of if-elseif-else blocks in a program.

Also note that, we can have if without elseif and else. But we can not have elseif and else without if!