Python 3 Program to Add Two Numbers Without Using + Operator in Terminal Tutorial For Beginners
0 views
Jun 1, 2025
Get the full source code of application here:
View Video Transcript
0:00
uh hello guys welcome to this video so
0:02
in this video I will show you a very
0:04
common program which is asked inside
0:06
competitive programming or interview
0:08
whenever you go for any Python interview
0:10
this question will be asked for you so
0:13
this is actually how to add two numbers
0:15
without using the plus operator without
0:18
using the standard addition operator
0:20
that you use a plus b you don't need to
0:23
use that operator still you need to add
0:25
two numbers so I will show you this
0:27
program that I will explain you step by
0:30
step so first of all inside Python we
0:34
take two numbers number one I give it a
0:39
seven number two I give it a value of
0:42
five and now I just need to define a
0:46
function which will be
0:49
add without plus and here we pass these
0:53
two numbers to this function number one
0:55
number two so now we just need to define
0:57
this function so this function
1:00
essentially will not use this plus
1:05
operator so now inside this
1:10
function in the standard way what we do
1:13
we use this plus operator which simply
1:16
add these two numbers so the result
1:18
comes out to be 12 so if I try to print
1:21
this this is a standard way by which you
1:25
add two numbers as you will see the
1:28
result comes out to be 12 but in
1:30
interview in competitive programming
1:32
there will be this question will be
1:34
asked that you don't need to use the
1:36
standard plus operator instead you need
1:39
to
1:41
uh still add these two numbers without
1:44
using it so now the approach is very
1:47
simple here you first of all need to use
1:50
some bitwise operations such as let me
1:53
show you so here we simply put while and
1:57
we basically check here while the B
1:59
value is not equal to
2:02
zero so here we put this condition
2:05
inside while loop we are checking this
2:08
if the number two which is passed here
2:10
which is five it's not equal to zero
2:13
then in that case we need to carry out
2:16
these three process first of all we need
2:19
to execute this a and b this is this is
2:24
a special operator which is called as uh
2:27
or
2:29
operator which is called as s xor zor
2:34
operator so first of all it actually
2:37
convert both these numbers into binary
2:40
which is how the CPU identify these
2:44
numbers into binary 011 form both these
2:48
numbers are converted into binary and
2:50
then it carries calculate the carry and
2:53
it stored it inside this variable if I
2:56
want to just print out this carry you
3:00
can print it out and then we also do
3:04
this a this is a special operator which
3:07
is the and operator we do this part as
3:11
well and then b is equal to carry and
3:15
this is a special operator one and then
3:19
after that we return this value of
3:22
a so if you try to run this program you
3:26
will see the result still comes out to
3:28
be 12 as you can
3:31
see let me just eliminate this print
3:35
statement and just say you can see the
3:38
result comes out to be 12 here if I
3:41
change this let's suppose I say 1 and 1
3:46
so 1 + 1 comes out to be two you can see
3:49
so this program basically we haven't
3:51
using the plus operators still we are
3:53
adding the two numbers so here these
3:56
three operations we are doing it bitwise
3:58
operators we are using the zor operator
4:01
and
4:02
the and operator this is the and
4:05
operator that you are seeing this is the
4:07
zor operator so both these numbers are
4:10
converted into binary what we are not
4:13
adding 1 + 1 we are converting this one
4:16
into binary 1 0 1 form and then we are
4:20
calculating the carry here first of all
4:21
then we are putting the and operator and
4:24
then again carry and then we are
4:27
returning it so this is essentially the
4:29
program you can pause and write this
4:32
full program also check out my website
4:35
freemediattools.com
4:37
uh which contains uh thousands of tools