DEV Community

G Gokul
G Gokul

Posted on Edited on

Python tasks - 5

Task - A number is a perfect number if the sum of its factors other than the given number gives the same number.

For example, 6 is a perfect number, since
adding the factors of 6 (other than 6), namely 1, 2 and 3 gives the given number 6.
i.e.,1+2+3 = 6 is the given number.

Program:

no =6
total=0
factor = 1
while factor<6:
    if(no%factor==0):
       total=total+factor 
    factor=factor+1
if(no==total):
    print("perfect number")
else:
    print("not perfect")
Enter fullscreen mode Exit fullscreen mode

Flowchart:
on

Output:
perfect number

1) Divisible by 2:

Take Last Digit.
It should be 0,2,4,6 or 8.

Program:

no = 1212129
last_digit = no%10 
if last_digit in (0,2,4,6,8):
    print('Divisible by 2')
else:
    print("not divisible by 2")
Enter fullscreen mode Exit fullscreen mode

Output:
not divisible by 2

2) Divisible by 3:

The sum of any three consecutive numbers is divisible by 3.
33+34+35 = 102

Program:

no = 15
ones_place = no % 10
tens_place = no // 10 
sum = ones_place + tens_place
if sum%3 ==0:
    print("Divisible by 3")
else:
    print("not divisible by 3")

Enter fullscreen mode Exit fullscreen mode

Output:
Divisible by 3

3) Divisible by 4:

Program:

no = 124
last_two_digits = no % 100
if last_two_digits % 4 == 0:
    print("divisible by 4")
else:
    print("not divisible by 4")
Enter fullscreen mode Exit fullscreen mode

Output:
divisible by 4

4) Divisible by 5:

Program:

no = 125
last_digit = no % 10
if last_digit in (0,5):
    print('Divisible by 5')
else:
    print("not divisible by 5")
Enter fullscreen mode Exit fullscreen mode

Output:
Divisible by 5

5) Divisible by 6:

Program:

no = 126
if no % 2 == 0 and no % 3 == 0:
    print('Divisible by 6')
else:
    print('Not divisible by 6')
Enter fullscreen mode Exit fullscreen mode

Output:
Divisible by 6

6) Divisible by 7:

Program;

no = 126
last_digit = no % 10
remaining = no // 10
result = remaining - (last_digit * 2)
if result % 7 == 0:
    print('Divisible by 7')
else:
    print('Not divisible by 7')
Enter fullscreen mode Exit fullscreen mode

Output:
Divisible by 7

7) Divisible by 8:

Program:

no = 1234
last_3_digits = no % 1000
if last_3_digits % 8 == 0:
    print("Divisible by 8")
else:
    print('not divisible by 8')
Enter fullscreen mode Exit fullscreen mode

Output:
not divisible by 8

8) Divisible by 9:

Program:

no = 126
last_two = no % 100
first_digit = no // 100
sum = first_digit + (last_two // 10) + (last_two % 10)
if sum % 9 == 0:
    print('Divisible by 9')
else:
    print('Not divisible by 9')
Enter fullscreen mode Exit fullscreen mode

Output:
Divisible by 9

9) Divisible by 10:

Program:

no = 120
last_digit = no % 10
if last_digit == 0:
    print('Divisible by 10')
else:
    print('Not divisible by 10'
Enter fullscreen mode Exit fullscreen mode

Output:
Divisible by 10

10) Divisible by 11:

Program:

no = 121
last_two = no % 100
first_digit = no // 100
odd = first_digit + (last_two % 10)
even = last_two // 10
if (odd - even) % 11 == 0:
    print('Divisible by 11')
else:
    print('Not divisible by 11')
Enter fullscreen mode Exit fullscreen mode

Output:
Divisible by 11

11) Divisible by 12:

Program:

no = 120
if no % 3 == 0 and no % 4 == 0:
    print('Divisible by 12')
else:
    print('Not divisible by 12')
Enter fullscreen mode Exit fullscreen mode

Output:
Divisible by 12

Top comments (0)