Python 3 Program to Add Two Numbers Using Recursion in Terminal Tutorial For Complete Beginners
417 views
Jun 1, 2025
Get the full source code of application here:
View Video Transcript
0:00
uh hello guys uh welcome to this video
0:02
So in this video I will show you a very
0:04
important competitive program which is
0:07
asked inside interviews Whenever you go
0:10
to interviews it will be asked what is
0:12
the way by which we can add two numbers
0:14
in Python using the concept of recursion
0:17
Recursion is a concept in which we call
0:19
the same function
0:22
inside let me show you the concept where
0:24
we call the function selflessly So we
0:29
have a function inside Python here So
0:32
let's suppose I define this recursive
0:37
function and uh I pass a value here of a
0:42
and here I will simply compare this
0:44
value If a reaches to zero then we break
0:48
out of this loop and we simply print
0:50
out hello and we simply break until
0:56
that we actually call the same function
1:00
right here we simply decrement the value
1:03
a minus one So essentially we declared
1:06
this recursive function
1:08
We take this input as an argument here
1:11
the value of a Then we compare it inside
1:14
this if condition that if the value of a
1:16
reaches to zero then we print this
1:20
statement of hello and we break out of
1:22
this function and until that we
1:25
recursively call this function the same
1:27
function and decrement the value of a So
1:30
now let me just call this function So if
1:33
I
1:34
say the value of a initial value of a is
1:39
five So now the how the function will
1:42
execute So you can just see whenever I'm
1:44
calling this function the initial value
1:47
of a is equal to five So it will reach
1:50
right here It will be five
1:53
here And now this condition will
1:55
evaluate to false here This will not be
1:58
executed Then it will it will come here
2:01
So the a value will be decremented So 5
2:04
- 1 So this will be four So this will
2:07
now
2:08
be
2:10
four So this will again come right here
2:13
4 -
2:15
1 This will be three So this again will
2:19
be 3 This will be 3 - 1 Now this will be
2:24
2 Again this will come So 2 - 1 is equal
2:29
to 1 And again it will come 1 -
2:33
1 So this will reach this condition here
2:36
It the a will become zero And now at
2:40
that time we will get this hello
2:44
statement So we can even print
2:47
out the value of a So if I try to
2:51
execute this program if you
2:54
see so it is saying that you should
2:58
have
3:01
indentation sorry we should have this
3:04
else statement right
3:26
Good Let me just delete this and again
3:29
run this
3:36
Uh let me just correct this error I
3:39
think there is some kind of
3:43
error Let me ask JPT here It
3:50
will okay
3:55
Yes Break outside
3:59
loop Sorry This needs to be inside
4:03
corrected version
4:06
So
4:09
sorry So now we simply return from this
4:12
function by writing the return keyword
4:14
not break Break is not there inside
4:16
Python If you want to exit out the
4:19
function we simply write return right
4:20
here So now what happens If you see if I
4:24
try to print out the value of a as well
4:27
Initial value of a will be five And as
4:30
you can see the output comes initial
4:32
value is five then it becomes four 3 2 1
4:36
and then at last we print out the value
4:39
of hello So this is actually the concept
4:42
of recursion and using the similar
4:45
concept we will be adding two
4:47
numbers So this function will now change
4:51
here So we will simply
4:54
define this function which will take two
4:57
values a comma
4:59
b and here inside this we will check for
5:03
the value of
5:04
b If it
5:07
reaches zero then we simply return the
5:13
a and then in the else scenario
5:18
we again call the same function
5:21
recursive at and this is the logic of
5:24
this we increment the value of a by one
5:27
and decrement the value of b by one So
5:32
here we are incrementing it a + 1 and
5:35
decrementing the value of v b minus one
5:38
So this is the way by which we
5:41
recursively add two numbers inside
5:43
python So if I try to call this function
5:47
initial value of a is let's suppose five
5:51
and b is
5:53
three So now the result will be 5 + 3 8
5:57
But let me show you how it executes So
5:59
if I go to the terminal and just add So
6:04
now the result comes out to be eight So
6:07
how it executes this is the most
6:09
important thing So now initial value as
6:12
you can see the a value is 5 b is 3 So
6:16
initial is a is equal to 5 b is equal to
6:21
3 So now we come to our
6:24
function So here b is not equal to zero
6:28
So this will not execute We come to this
6:31
We again call this function So we
6:33
increment So now the value of a becomes
6:36
5 + 1 is equal to 6 and b becomes 3 - 1
6:41
2 So 6 2 We again call this function So
6:47
b is not equal to 0 So again it will
6:50
ignore this And again 6 +
6:54
1 comes out to be 7 And again 2 - 1
6:59
comes out to be 1 So 7A
7:03
1 and then again it will call itself So
7:07
b is not again zero So again it will
7:09
come to this function again 7 +
7:13
1 is equal to 8 and then 1 - b value is
7:20
1 So 1 - 1 is equal to zero So now the
7:25
argument becomes
7:27
8 comma
7:30
0 So now if we are checking here if B's
7:34
value is equal to zero So in this case B
7:37
is zero So now we are returning A which
7:39
is 8 So we are simply returning from
7:42
this function return is equal to and
7:45
printing out 8 So in this way we are not
7:47
directly adding 5 + 3 but instead we are
7:51
in incrementing a every time and
7:54
decrementing b This is the mathematical
7:57
logic behind recursively adding two
7:59
numbers So if you want to add two
8:02
numbers essentially the logic is we
8:05
increment every time this number and
8:09
decrementing this one So 5 + 1 6 and 3 -
8:14
1 2 So essentially we're transferring
8:18
this to this
8:20
So this is the logic how we can add
8:23
recursively two numbers This is a
8:25
program So I hope that I have explained
8:29
you perfectly in this video Uh thank you
8:32
very much for watching this video and
8:34
also check out my website
8:37
freemediattools.com uh which contains uh
8:40
thousands of tools
#Computer Education
#Scripting Languages