1
00:00:00,950 --> 00:00:07,799
We have seen comparison operators, like less
then, equal to, less than or equal.
2
00:00:07,799 --> 00:00:15,849
to and so, on. We will see bunch of few more
operators in this session. So, let
3
00:00:15,849 --> 00:00:20,680
us consider the modulo operator which we have
already seen in when we discussed
4
00:00:20,680 --> 00:00:22,680
utility in GCD.
5
00:00:22,680 --> 00:00:31,670
So, a percentage b gives the reminder when
a is divided by b. So, suppose we have the
6
00:00:31,670 --> 00:00:37,660
following problem, we get a number a and we
want to check whether the given number is
7
00:00:37,660 --> 00:00:44,960
divisible by 6. If it is divisible by 6 a
modulo 6 will be 0 the reminder will be 00.
8
00:00:44,960 --> 00:00:57,200
So, we will write a simple code, you have
int a, a is of type int. Then, scan the number
9
00:00:57,200 --> 00:01:05,790
using scanf percentage d and a. And then,
you test whether a is divisible by 6 to test
10
00:01:05,790 --> 00:01:12,880
whether a is divisible by 6 you check whether
a mod 6 is 0. If it is divisible, you say
11
00:01:12,880 --> 00:01:21,460
that input is divisible by 6 percentage d
a. Otherwise, else, printf the input is not
12
00:01:21,460 --> 00:01:26,060
divisible by 6 very simple operation.
13
00:01:26,060 --> 00:01:33,590
Now, let us make it slightly more elaborate.
Suppose, you have to test whether, this a
14
00:01:33,590 --> 00:01:41,229
slight variant. Suppose, you have to test
whether a give number is divisible by 6 and
15
00:01:41,229 --> 00:01:50,159
by 4 two numbers. How do you do this? So,
you scanf the number and you test whether
16
00:01:50,159 --> 00:02:00,479
a is divisible by 6. So, a modulo 6 is 0.
If that is true, then you also check whether
17
00:02:00,479 --> 00:02:09,429
a modulo 4 is 0. If both are true, then you
print that the given number is divisible by
18
00:02:09,429 --> 00:02:14,319
6 and 4. So, percentage is divisible by 6
and 4 a.
19
00:02:14,319 --> 00:02:20,459
So, you can argue about this program and see
that, if it is divisible by 6, but not by
20
00:02:20,459 --> 00:02:27,690
4 then, it will enter the first if, but not
enter the second if. Therefore, it will not
21
00:02:27,690 --> 00:02:34,310
print that it is divisible by 6 and 4. Similarly,
if it is not even divisible by 6 it will not
22
00:02:34,310 --> 00:02:40,470
even enter the first if condition. So, you
will in any case not print that it is divisible.
23
00:02:40,470 --> 00:02:47,930
So, convince yourself that this particular
code will print a number is divisible by 6
24
00:02:47,930 --> 00:02:57,739
and 4 if and only if the given number is divisible
by both 6 and 4.
25
00:02:57,739 --> 00:03:05,019
Now, that piece of code was slightly long
is there any way to write the same code with
26
00:03:05,019 --> 00:03:12,379
a fewer number of lines. And for this c provides
what are known as logical operators. Now,
27
00:03:12,379 --> 00:03:20,760
there are three logical operators in Boolean
logic which are Boolean AND, Boolean OR and
28
00:03:20,760 --> 00:03:27,939
Boolean NOT. So, there are three logical operations
AND, OR and NOT and C provides all of them.
29
00:03:27,939 --> 00:03:35,499
So, the same if condition that we wrote before,
we could have easily said if it is divisible
30
00:03:35,499 --> 00:03:41,470
by 6 and if it is divisible by 4 then print
the output.
31
00:03:41,470 --> 00:03:53,969
So, for this C provides an operator which
is the Boolean AND operation. So, the Boolean
32
00:03:53,969 --> 00:04:04,190
AND operation in C is given by two ANDs. So,
by know you should be familiar with the fact
33
00:04:04,190 --> 00:04:12,799
it certain operations in C have repeated characters.
For example, we already have seen the equality
34
00:04:12,799 --> 00:04:18,540
operations which was equal, equal. Similarly,
the Boolean operation and it is actually the
35
00:04:18,540 --> 00:04:24,140
and symbol on the keyboard. But, you have
to have two of them that represents the logical
36
00:04:24,140 --> 00:04:30,670
AND.
So, this expression says if a modulo 6 is
37
00:04:30,670 --> 00:04:41,230
0. So, this expression is what test for a
is a multiple of 6. And this is the expression
38
00:04:41,230 --> 00:04:50,390
which test whether a is the multiple of 4.
So, if both conditions are true, then you
39
00:04:50,390 --> 00:05:01,600
say that the given number is divisible by
6 and by 4. So, consecutive ampersand signs,
40
00:05:01,600 --> 00:05:06,480
that is the and symbols without any blanks
in between is the C operator corresponding
41
00:05:06,480 --> 00:05:14,940
to the mathematical and the logical AND function.
So, it takes two values as input and returns
42
00:05:14,940 --> 00:05:23,030
a 0. If any of the values is 0, if both values
are 1 then it returns a 1. So, this is the
43
00:05:23,030 --> 00:05:29,760
same as the logical AND. If either of them
is 0 then the result is 0, if both of them
44
00:05:29,760 --> 00:05:32,710
are 1, then the result is 1.
45
00:05:32,710 --> 00:05:41,630
So, the truth table for the operation AND
is as follows if a is a non-zero value and
46
00:05:41,630 --> 00:05:49,610
b is a non-zero value, then C considers that
both are true. So, the output value is of
47
00:05:49,610 --> 00:06:01,460
a and b is 1. If a is 0 and b is any value
at all the output is 0 and b is not evaluated.
48
00:06:01,460 --> 00:06:11,210
So, this the same as logical end. The only
think to notice that, if in evaluating a and
49
00:06:11,210 --> 00:06:16,630
b you already know that a is 0, then you know
the result is 0. So, C will not bother to
50
00:06:16,630 --> 00:06:22,950
evaluate b. Because, it knows that the result
is already 0.
51
00:06:22,950 --> 00:06:35,590
Every expression has a type a and b is of
type int regardless of the types of a and
52
00:06:35,590 --> 00:06:42,300
b. This is because a and b is a logical assertion.
The type of a logical assertion is that, it
53
00:06:42,300 --> 00:06:48,971
is either true or false, it that it corresponds
to a Boolean value. Therefore, at the type
54
00:06:48,971 --> 00:06:56,210
of an a and then b regardless of what a and
b are the result is always 0 or 1. So, it
55
00:06:56,210 --> 00:07:02,360
is of type int. Now, you can print the result
as an int, you can say printf percentage d
56
00:07:02,360 --> 00:07:04,560
a and then b.
57
00:07:04,560 --> 00:07:12,610
Now, there are three logical operations as
I mention. So, there is also OR in c it is
58
00:07:12,610 --> 00:07:21,370
denoted by two vertical bars which are there
on your keyboard. So, a or b which is a bar
59
00:07:21,370 --> 00:07:33,620
bar b evaluates to non-zero if either a is
non-zero or b is non-zero. If both of them
60
00:07:33,620 --> 00:07:38,350
are zero, then the result is zero. So, this
the meaning of a logical OR operation, if
61
00:07:38,350 --> 00:07:47,680
both of them are false, then a or b is false.
If at least one of them is true then a or
62
00:07:47,680 --> 00:07:52,350
b is true.
So, you can write the truth table for that.
63
00:07:52,350 --> 00:07:59,140
If a and b are 0 then the output is 0, if
a is non zero and b is any value. Then, in
64
00:07:59,140 --> 00:08:07,030
already know that the output of a or b is
1. So, the output is 1 and b will not be evaluated.
65
00:08:07,030 --> 00:08:12,180
This is similar to in the case of AND. If
a was 0 and b was any other value, then you
66
00:08:12,180 --> 00:08:17,790
know that the output of and is 0. Therefore,
b will not be evaluated. Similarly, if here
67
00:08:17,790 --> 00:08:26,540
if a is non-zero, then you know the value
is 1. So, b will not evaluated and as before
68
00:08:26,540 --> 00:08:28,780
a or b is of type int.
69
00:08:28,780 --> 00:08:37,969
So, the third logical operation is NOT. Now,
NOT in C is denoted as the exclamation mark.
70
00:08:37,969 --> 00:08:45,130
So, let us see an example of that. So, NOT
is the logical complement and it takes only
71
00:08:45,130 --> 00:08:53,950
one argument, this is different from the previous
two that we have seen a or b and a and b both
72
00:08:53,950 --> 00:09:00,080
took two arguments it is not takes only one
arguments. So, it is called a unary operators.
73
00:09:00,080 --> 00:09:07,780
So, NOT of a is an expression of type integer
and the value is the negation of a. So, if
74
00:09:07,780 --> 00:09:17,350
a is 0 NOT of a will be 1 and if a is non
zero then NOT of a will be 0.
75
00:09:17,350 --> 00:09:26,560
So, for example, if I want to say that a is
not divisible by 3 I will just write NOT of
76
00:09:26,560 --> 00:09:32,400
a mod 3 equal to 0. You know that a mod 3
equal to 0 test for a being divisible by 3.
77
00:09:32,400 --> 00:09:43,050
So, negation of that it will say that the
given number is not divisible by 3.
78
00:09:43,050 --> 00:09:52,550
Let us finish this by slightly complicated
example which is that of leap years. So, I
79
00:09:52,550 --> 00:10:02,740
am given a particular year number and I want
say whether the given number is a corresponds
80
00:10:02,740 --> 00:10:13,170
to leap year or not. Now, what is a leap year
it is that you add a few years will have February
81
00:10:13,170 --> 00:10:20,401
29th in February all other years will have
28 days in February. So, what is the logic
82
00:10:20,401 --> 00:10:34,240
of a leap year. So, roughly an average solar
year is 365.242375 rotations. So, in particular
83
00:10:34,240 --> 00:10:39,960
is not an integer.
So, we normally say that year has 365 days
84
00:10:39,960 --> 00:10:48,200
that is not quite true, this it is a rounding.
So, how much are we losing. So, you can calculate
85
00:10:48,200 --> 00:10:56,920
it as follows. The remaining number after
the decimal point is a roughly 0.25. So, at
86
00:10:56,920 --> 00:11:06,950
a rough cut let say that every 4 years. Because
of this 0.25 you will lose one day. So, every
87
00:11:06,950 --> 00:11:19,140
year you are losing about a quarter of a day.
So, if every 4 years you will add a day. Now,
88
00:11:19,140 --> 00:11:33,440
when you do that you go back to... So, just
a minute.
89
00:11:33,440 --> 00:11:50,360
So, you have 365 point let us say 25. So,
every 4 years you would lose about a day approximately
90
00:11:50,360 --> 00:12:13,190
1 day. So, in order to compensate for that
you add 1 day every 4 years. So, in 100 years
91
00:12:13,190 --> 00:12:29,320
you would have added 25 days. But, that is
1 day too much. Because, remember that this
92
00:12:29,320 --> 00:12:35,880
number is only 365.24 something. So, in 100
days you should have added only 24 days. But,
93
00:12:35,880 --> 00:12:53,070
now you added a 25 days. So, to compensate
for that every 100 year skip adding the extra
94
00:12:53,070 --> 00:13:06,360
day.
So, every 4 years you have add 1 extra day,,
95
00:13:06,360 --> 00:13:12,410
but every 100 years every 100th year you do
not add that extra day, you skip it. Because,
96
00:13:12,410 --> 00:13:18,840
you would have added 1 more day then you.
And then again you can look at what remains,
97
00:13:18,840 --> 00:13:29,890
what remains is roughly .24 which means that
every 400 years if you do this adjustment
98
00:13:29,890 --> 00:13:35,110
you are losing about a day. Because, every
100 years you are losing about quarter of
99
00:13:35,110 --> 00:13:45,680
a day from this 0.2375 part. So, you do the
same logic again every 400 years. So, every
100
00:13:45,680 --> 00:13:55,950
400 years add an extra day. So, this is the
logic of the leap year that we all know. So,
101
00:13:55,950 --> 00:14:01,779
how do you decide whether year will be leap
year.
102
00:14:01,779 --> 00:14:10,040
So, the logic that I have outlined just now
says that, if a year is a multiple of 4 then
103
00:14:10,040 --> 00:14:16,040
it is a leap year. But, if a year is a multiple
of 100 as well then it is not a leap year.
104
00:14:16,040 --> 00:14:22,650
But, if it is a multiple 400 than it is a
leap year. So, here is a pretty complicated
105
00:14:22,650 --> 00:14:29,220
expression. So, every 4th year is a leap year.
But, skip every 100th year unless it is also
106
00:14:29,220 --> 00:14:37,690
a 400th year. And you can write this expression
in C, it is slightly complex has you can imagine.
107
00:14:37,690 --> 00:14:46,500
So, if the first line the first expression
says that, wise year is a multiple of 4. So,
108
00:14:46,500 --> 00:14:54,990
if year is divisible by 4 also the following
should be true, it should not be a multiple
109
00:14:54,990 --> 00:15:03,540
of 100 unless it is a multiple of 400. So,
it should not be divisible by 100 that should
110
00:15:03,540 --> 00:15:11,230
be true or it should be true that, it should
be a multiple of 400. For example, if you
111
00:15:11,230 --> 00:15:18,529
have 400 then it is a leap year. So, what
will happen is that year modulo 4 400 modulo
112
00:15:18,529 --> 00:15:24,300
4 is 0.
Then, what happens is that you have 400 modulo
113
00:15:24,300 --> 00:15:36,899
100 that is 0. So, this is equal to 0 that
is 1 NOT of 1 is 0. So, this part is entirely
114
00:15:36,899 --> 00:15:45,520
0, but it is divisible by 400, 400 divided
by 400 is 0. Therefore, this part is true,
115
00:15:45,520 --> 00:15:55,959
this or 0 or 1 is true. Therefore, the whole
expression becomes 1 and 1. So, it is true.
116
00:15:55,959 --> 00:16:03,390
So, this logical expression slightly complicated
logical expression encodes the logic for saying
117
00:16:03,390 --> 00:16:08,980
that the given year is a leap year. So, try
this out yourself this is a slightly tricky
118
00:16:08,980 --> 00:16:17,350
expression. And convince yourself that, this
exactly encodes the logic of the leap year.