Program Logic/Algorithm - Verify a number is a perfect number & Print divisors of a No. | Part 16

preview_player
Показать описание
Try to Understand the Logic and Implement in Any Programming Language ( c/c++/java/python/c#/etc): Iteration/Repetitive/Loop Statement

if you like my video, please subscribe to my channel and share the video

22. Write a program to check whether a number is a Perfect number or not.
Ex - 6 divisors - 1, 2, 3 = 1 + 2 +3 = 6

num = 6;
sum = 0;
for(int i =1; i less than num ; i++){

if( num % i == 0 )
sum = sum + i;

}
if ( sum == num )
print "Perfect"
else
print "Not perfect"

Efficient Algorithm of Perfect Number :
int num = 6;
int sum = 0;
for (int i = 1; i less than equal num/2; i++) {
if (num % i == 0)
sum = sum + i;

}

if (sum == num)
print "Perfect"
else
print "Not Perfect"

Find out the Divisors of a number :

int num = ;
for (int i = 1; i less than equal num; i++) {

if (num % i == 0)
print i

}

Instagram: techtalk_debu

Thanks & Regards,
Debu Paul
Рекомендации по теме