How to Check Whether a Number is Perfect Number or Not in Python – GATE, Interview & Placement
37K 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
0:04
Python program which is asked commonly
0:06
inside uh interviews for programming or
0:09
competitive exams or placements
0:12
uh whether to check whether a number is
0:14
a perfect number or
0:16
not So it is commonly asked So first of
0:20
all I will show you what is actually a
0:22
perfect number So in mathematics perfect
0:25
number is that number which is uh let me
0:29
show you this is actually the python
0:31
program So
0:32
python app 8 as I run the program it
0:36
will ask you to enter a particular
0:38
number and if I just say 16 So I write
0:42
16 here So you will see 16 is not a
0:45
perfect number So first of all uh if you
0:48
google it what is perfect
0:51
number so perfect number is a positive
0:53
integer whose the sum of its proper
0:55
divisor excluding the number itself
0:58
equals the number itself So what do you
1:00
mean by that
1:03
so it is a number for example 6 is a
1:06
perfect number because it's proper
1:09
divisor such as 6 is divisible by
1:13
1 It's divisible by 2 2 3's are six and
1:16
it's also divisible by 3 So if you sum
1:20
all these numbers which is 1 + 2 + 3 it
1:23
comes out to be six So that is the
1:26
definition of a perfect number So if I
1:30
just input this number in this program
1:32
if I say six you will see six is a
1:34
perfect
1:35
number because the sum of all the
1:38
divisors excluding the number itself So
1:41
six is not included in this So it
1:44
basically comes in the table of 1 2 and
1:47
then three So 32 + 5 is equal to
1:52
6 So in this
1:55
way so we actually
1:59
uh written this program here So now let
2:01
me show you step by step
2:04
uh just using this Python tutor website
2:07
where you can visualize the program step
2:10
by step So here the very first step here
2:12
you need to provide the number So if I
2:15
provide six here you will see the global
2:18
variable has been created The number has
2:20
six variable So this variable is created
2:24
It has holding the value of
2:26
six And now for detecting whether this
2:30
number is a perfect number or not We
2:31
need some variables So we declared this
2:35
variable sum of divisor to keep track of
2:37
the sum Initially the value is zero So
2:40
another variable is created sum of
2:42
divisor is zero And now we run a simple
2:45
for loop for i in range 1 to the number
2:50
So number is not included here So it
2:53
will run until 1 to 5 So because the
2:57
actual number is not included right here
2:59
So it will run up
3:01
till 6 - 1 So it will run up to five and
3:07
it will there is the simple condition
3:09
right put here We are simply checking
3:12
that if the remainder comes out to be
3:14
zero So we are simply checking if
3:17
one So for the very first thing I will
3:21
be one Here you can see I So we are
3:23
simply checking that if six is divisible
3:26
by one So as you can see it is divisible
3:30
So remainder comes out to be zero So
3:32
this condition will evaluates to
3:35
true and then this one will be added to
3:39
this sum of divisor So this will this
3:42
value will change from 0 to
3:44
1 So now it will iterate to the next
3:47
condition here It will now I will become
3:50
two Again it will check 6 is divisible
3:54
by two Again it will add this come to
3:57
this and 1 + 2 it will become
4:01
three So you can see that now it will
4:04
move to the next iteration So I will be
4:07
now three Three again is divisible by 6
4:11
It will come out Remainder is zero So
4:13
again three will be added from here 3 +
4:16
3
4:18
6 So now sum of divisor becomes six Now
4:23
four is
4:24
not 6 is not divisible by four So
4:27
condition will be false It will move to
4:30
five Five is not divisible by six So now
4:33
as soon as I reaches six so the loop
4:36
will end And then we'll simply compare
4:38
that if this variable is equal to the
4:41
number itself then this is actually a
4:44
perfect number So we print out this
4:46
result six is a perfect
4:47
number And now let's suppose if I take
4:51
uh a
4:52
[Music]
4:55
example a number which is not
4:58
divisible Let's suppose I
5:03
take four as example So I taken
5:06
four So again we'll be checking
5:09
here I will be one So I is four is
5:13
divisible by one So sum of divisor one
5:17
will be added Now two 2 is also
5:20
divisible So
5:21
two will be added to this variable So 2
5:25
+ 1 3 Now again we'll be checking three
5:28
is not divisible by
5:30
four So it will come out And now you'll
5:34
be checking So three is not equal to
5:36
four So this else condition will be
5:39
printed that four is not a perfect
5:41
number You can see
5:42
that So in this way this is the logic
5:46
here mathematically defined what is a
5:49
perfect number So in this way you can
5:52
explain it It's very commonly asked
5:54
question in programming interviews and
5:56
competitive exams And thank you very
5:59
much for watching this video and also
6:01
check out my website freemediatools.com
6:04
uh which contains thousands of tools
#Computer Education
#Programming
#Scripting Languages