Asst. Prof.
Zeynep TAVUKOĞLU ŞAHİN
Email
[email protected]
İstanbul Okan University
2023-2024 Fall
§ An expression containing logical operator returns either 0
or 1 depending upon whether expression results true or
false. Logical operators are commonly used is decision
making in C programming.
Example #5: // C Program to demonstrate the working of logical
operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
result = (a = b) && (c > b);
printf("(a = b) && (c > b) equals to %d \n", result);
result = (a = b) && (c < b);
printf("(a = b) && (c < b) equals to %d \n", result);
result = (a = b) || (c < b);
printf("(a = b) || (c < b) equals to %d \n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) equals to %d \n", result);
result = !(a != b);
printf("!(a != b) equals to %d \n", result);
result = !(a == b);
printf("!(a == b) equals to %d \n", result);
return 0; Output:
}
(a = b) && (c > b) equals to 1
(a = b) && (c < b) equals to 0
(a = b) || (c < b) equals to 1
(a != b) || (c < b) equals to 0
!(a != b) equals to 1
!(a == b) equals to 0
§ (a = b) && (c > 5) evaluates to 1 because both operands (a = b) and (c >
b) is 1 (true).
§ (a = b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).
§ (a = b) || (c < b) evaluates to 1 because (a = b) is 1 (true).
§ (a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c <
b) are 0 (false).
§ !(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a
!= b) is 1 (true).
§ !(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is
0 (false).
§ A conditional operator is a ternary operator, that
is, it works on 3 operands.
Conditional Operator Syntax:
conditionalExpression ? expression1 : expression2
The conditional operator works as follows:
§ The first expression conditionalExpression is
evaluated at first. This expression evaluates to 1 if
it's true and evaluates to 0 if it's false.
§ If conditionalExpression is true, expression1 is
evaluated.
§ If conditionalExpression is false, expression2 is
evaluated.
#include <stdio.h>
int main(){
char ….....;
int ….....;
printf("….........................");
scanf("%c",&February);
// If test condition (February == 'l') is true, days equal to
29.
// If test condition (February =='l') is false, days equal to
28.
days = (…........... == '….......') ? ….... : …..........;
printf("Number of days in February = %d",days);
return 0;
#include <stdio.h>
int main(){ Output:
If this year is leap year, enter l. If not
char February; enter any integer: l
int days; Number of days in February = 29
printf("If this year is leap year, enter l. If not enter any
integer: ");
scanf("%c",&February);
// If test condition (February == 'l') is true, days equal to
29.
// If test condition (February =='l') is false, days equal to
28.
days = (February == ‘l') ? 29 : 28;
printf("Number of days in February = %d",days);
return 0;
Activity #1:
C Program to find odd or even using conditional
operator (entered by the user)
Activity #1: C Program to find odd or even using
conditional operator
#include <stdio.h>
int main()
{
int n;
printf("Input an integer\n");
scanf("%d", &n);
n%2 == 0 ? printf("Even\n") : printf("Odd\n");
return 0;
}
Activity #2:
C Program to find largest among two numbers (entered
by the user)
Activity #2: C Program to find largest among two
numbers (entered by the user)
#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter two numbers: ");
scanf("%d %d",&a,&b);
c=a>b?a:b;
printf("The largest number is %d",c);
return 0;
}
Activity #3:
C program to find the area of a triangle, given three sides
(entered by the user)
Activity #3: C program to find the area of a triangle, given
three sides (entered by the user)
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c, u, area;
printf("Enter the values of a, b and c: \n");
scanf("%f %f %f", &a, &b, &c);
/* compute u */
u = (a + b + c) / 2;
area = sqrt(u * (u - a) * (u - b) * (u - c));
printf("Area of a triangle = %f \n", area);
}