馃攳
C++ Even or Odd Number Program | CPP video Tutorial - YouTube
Channel: LearningLad
[0]
Hi this is Anil from Learning Lad and welcome
to another video on C++ programming language.
[6]
In this video we will see how we can write
a c++ program to check the integer number
[12]
entered by the user is an even number or a
odd number.
[17]
So before getting started we will see what
is an even number and what is a odd number.
[24]
An even number is an integer which is evenly
divisible by 2.
[28]
which is nothing but let鈥檚 say n is the
number and if you divide this number n by
[35]
2 and if you get the remainder as zero, then
this n is an even number and if the remainder
[45]
is a non zero value then this n is the odd
number.
[52]
For example if we have the number 6 here and
we are going to divide this 6 by 2
[60]
So we will get 2 * 3 = 6 and the most important
thing is this remainder value is zero.
[67]
So this 6 is an even number.
[71]
Similarly if we have the number let鈥檚 say
7 then we will divide this number 7 by 2.
[78]
2 * 3 = 6 and we get the remainder as 1 and
since this remainder is 1, which is a non
[86]
zero value, this number 7 is odd number.
[91]
So now how we can write a C++ program to check
the number entered by the user is an even
[97]
number or the odd number.
[99]
so what we're going to do is we are going
to ask the user to enter the integer number.
[104]
and then we are going to divide that number
by 2 and we calculate the remainder value.
[112]
if that reminder value is zero then we say
that the number entered by the user is an
[117]
even number and if that remainder is a non
zero value then we say that the number entered
[124]
by the user is the odd number.
[127]
so now how we can write the C++ program.
[131]
Here I have already written the Skeleton for
this program.
[134]
I have included this iostream header file
and I have included this STD namespace and
[141]
then we have this main function here.
[144]
So first what we're going to do is we are
going to declare the variables that we are
[148]
going to need in this program.
[151]
since we are working with the integer numbers
our variables are going to be of type integers.
[158]
The first variable that we need is for storing
the number entered by the user to check for
[164]
even or odd.
[166]
now I will name that variable as number and
then we need another variable to store the
[174]
remainder value when we divide this number
by 2 and I will name that variable as remainder.
[184]
OK
[185]
now we will ask the user to enter the number.
[189]
for that I will use this cout and I will say
please enter an integer number.
[200]
Ok
now the next thing that we are going to do
[202]
is we are going to read the number entered
by the user.
[207]
for that I will use the cin and we are going
to store that in this number variable.
[215]
Ok
[216]
now we have the number to check for even or
odd and the next thing that we need to do
[222]
is we need to calculate the remainder value.
[226]
so to calculate the reminder value we are
going to use the modulus operator.
[235]
this modulus operator is denoted by this percentage
symbol.
[240]
so Here we need to write number modulus 2.
[247]
So here what happens is it will perform number
divided by 2 and it will get that remainder
[255]
value and it will return that.
[258]
This statement will give the remainder when
we do number divided by 2 and then we are
[264]
going to store that remainder value in our
remainder variable.
[270]
so this statement will perform number divided
by 2 and whatever that remainder value we
[278]
get that will be stored in this remainder
variable.
[282]
The next thing that we need to do is we need
to check for the remainder variables value.
[287]
what value it is containing.
[288]
whether it is containing zero or non zero.
[292]
if it is containing a value of zero, it means
the number entered by the user is an even
[298]
number.
[299]
if it is a non zero value then it means that
the number entered by the user is the odd
[305]
number.
[306]
so we do that by using the if statement and
the condition for this if statement will be
[311]
remainder equal to 0.
[316]
remember that I'm using the double equal to
here.
[320]
this double equal to sign is used for comparing
the value.
[325]
this is the comparison operator.
[327]
so what we are doing here is we are checking
whether this remainder variable is containing
[332]
a value of 0.
[334]
so if this is so or if this condition is true
then we will print out that the number entered
[344]
by the user is an even number.
[347]
so we will say number is an even number.
[352]
Else it means that the remainder variable
is not containing the value of zero.
[358]
so we will have the else part here and we
will use a cout and we say I'm going to copy
[365]
this statement and I will paste it here in
this else part and I will say is a odd number.
[375]
so here we are checking the value stored in
this remainder variable here.
[380]
if it is containing a value of zero then it
means that the number entered by the user
[385]
is the even number and we are displaying that.
[388]
if this reminder variable is containing a
non zero value, then the else part will be
[393]
executed and here we are saying that the number
is odd number.
[399]
Ok
[400]
now I will save this program and I'll go to
build, build and run and here it says please
[406]
enter and integer number
[410]
just made a spelling mistake here
[411]
we should have an
[415]
So i鈥檓 gonna change that one to an instead
of and then I will perform this build and
[420]
run.
[422]
please enter an integer number.
[424]
I will enter the number 6 and if I hit the
enter button in my keyboard it says 6 is an
[430]
even number.
[431]
ok let's close this.
[433]
Let鈥檚 run this again and this time I will
enter 7.
[437]
it says 7 is a odd number.
[442]
so it means that this program is working properly.
[445]
Ok
[446]
now the thing is hereI have written this program
as simple as possible.
[452]
you can write this program without using this
remainder variable.
[456]
so just to show that I will remove this remainder
variable here and then I will remove this
[461]
statement and here we are going to check the
remainder in this if condition only.
[467]
for that I'll use parentheses then we will
write that condition right here.
[473]
number Mod 2 and then have the other things
as the same.
[478]
so here we are not using the remainder variable.
[481]
we are just performing the step right here
in this if condition,
[485]
so what this will do is it will first calculate
the remainder when the value stored in this
[492]
number variable is divided by 2 and then it
will check that reminder value with zero.
[499]
whether that reminder value is 0 or not.
[502]
so if that reminder value is zero then this
if condition will be executed.
[505]
else this else part will be executed.
[509]
So i鈥檒l just save this.
[511]
build and run this.
[513]
I鈥檒l just enter the number like 5.
[517]
it says 5 is odd number and I will run this
one more time.
[521]
this time I'll enter 2.
[523]
It says e 2 is an even number.
[526]
So this is it guys.
[528]
this is how you guys can write a simple program
in C++ to check the number entered by the
[533]
user is an even number or an odd number.
[536]
now if you like this video then hit the like
button if you don't like it then hit the dislike
[540]
button.
[541]
if you want to watch more tutorials like this
then do subscribe to my channel.
[544]
thanks for watching and I'll see you later
in the next video.
Most Recent Videos:
You can go back to the homepage right here: Homepage





