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
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.
#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.
#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)
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.
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.
#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).
#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).
#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.
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!
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; });
});
});
$(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
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;
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.
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!