Prime Number
# Prime Number
n=int(input('Enter a number: '))
div = 2
while div < n:
if n % div == 0:
print(' Not a Prime Number')
break
div+=1
else:
print('Prime Number')
Output:
Enter a number: 5
Prime Number
Enter a number: 4
Not a Prime Number
To check factorial
# check factorial
n=int(input('Enter a number: '))
total = 0
factor = 1
while factor < n:
if n % factor == 0:
total = total + factor
factor += 1
if total == n:
print("Perfect Number")
break
else:
print("Not a Perfect Number")
Output:
Enter a number: 8
Not a Perfect Number
Enter a number: 4
Perfect Number
20) 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.
Check whether 28, 54 and 496 are perfect numbers or not.
n=int(input('Enter a number: '))
total = 0
factor = 1
while factor < n:
if n % factor == 0:
total = total + factor
factor += 1
if total == n:
print("Perfect Number")
break
else:
print("Not a Perfect Number")
Output:
Enter a number: 6
Perfect Number
Enter a number: 28
Perfect Number
Enter a number: 54
Not a Perfect Number
Enter a number: 496
Perfect Number
Express 42 and 100 as the sum of two consecutive primes.
42 = 19 + 23
19 and 23 are consecutive primes
100 = 47 + 53
47 and 53 are consecutive primes
Divisible by 2
Last number will be 2,4,6,8,0
# Divisible by 2
n = int(input('Enter a number: '))
last_digit = n%10
if last_digit in (0,2,4,6,8):
print('Divisible by 2')
else:
print('Not Divisible by 2')
Output:
Enter a number: 23
Not Divisible by 2
Divisible by 3
The sum of number divisible by 3
n = int(input('Enter a number: '))
if n % 3 == 0:
print('Divisible by 3')
else:
print('Not Divisible by 3')
Output:
Enter a number: 5
Not Divisible by 3
no = 15
ones_place = no % 10 #5
tens_place = no // 10 #1
sum = ones_place + tens_place
if sum%3 ==0:
print("Divisible by 3")
Output:Divisible by 3
Divisible by 4
last two digit divided by 4
# Divisible by 4
n = int(input("Enter a number: "))
last_two = n % 100
if last_two % 4 == 0:
print("Divisible by 4")
else:
print("Not Divisible by 4")
Output:
Enter a number: 432
Divisible by 4
Divisible by 5
last digit will be 0 or 5
# Divisible by 5
n = int(input("Enter a number: "))
last_digit = n % 10
if last_digit == 0 or last_digit == 5:
print("Divisible by 5")
else:
print("Not Divisible by 5")
Output:
Enter a number: 999
Not Divisible by 5
Divisible by 6
Given number will be divided by 2 and 3
n = int(input("Enter a number: "))
if n % 2 == 0 and n % 3 == 0:
print("Divisible by 6")
else:
print("Not Divisible by 6")
Output:
Enter a number: 36
Divisible by 6
Divisible by 8
last three digit divided by 8
# Divisible by 8
n = int(input("Enter a number: "))
last_three = n % 1000
if last_three % 8 == 0:
print("Divisible by 8")
else:
print("Not Divisible by 8")
Output:
Enter a number: 6789
Not Divisible by 8
Divisible by 9
sum of digit divided by 9
n = int(input("Enter a number: "))
sum_digits = 0
while n > 0:
digit = n % 10
sum_digits = sum_digits + digit
n = n // 10
if sum_digits % 9 == 0:
print("Divisible by 9")
else:
print("Not Divisible by 9")
Output:
Enter a number: 81
Divisible by 9
Divisible by 10
last digit will be 0
# Divisible by 10
n = int(input("Enter a number: "))
last_digit = n % 10
if last_digit == 0:
print("Divisible by 10")
else:
print("Not Divisible by 10")
Output:
Enter a number: 65
Not Divisible by 10
Divisible by 11
sum of even-odd=0
n = int(input("Enter a number: "))
even_sum = 0
odd_sum = 0
position = 1
while n > 0:
digit = n % 10
if position % 2 == 0:
even_sum = even_sum + digit
else:
odd_sum = odd_sum + digit
n = n // 10
position += 1
difference = even_sum - odd_sum
if difference == 0 or difference % 11 == 0:
print("Divisible by 11")
else:
print("Not Divisible by 11")
Output:
Enter a number: 1224
Not Divisible by 11
Top comments (0)