DEV Community

Kuhanraja A R
Kuhanraja A R

Posted on

day -14 at payilagam exercises

Prime Numbers:
Numbers which are divisible by 1 and itself are called as prime numbers.(Eg-->3,5,7)

1) Find prime number or not:

no = int(input("Enter no. "))
div = 2
while div<no:
    if no%div == 0:
        print("Not Prime")
        break
    div+=1
else:
    print("Prime")

Output:

1)Enter no. 5
  Prime
2)Enter no. 6
  Not Prime
Enter fullscreen mode Exit fullscreen mode

2) Reversing the input number and find whether that reversed number is prime number or not:

def reverse_a_no(no):
    reverse = 0
    while no>0:
        rem = no%10
        reverse = (reverse*10) + rem
        no//=10 
    return reverse

no = int(input("Enter no. ")) 
reversed_no = reverse_a_no(no) 
print(reversed_no)
def find_prime(no):
    div = 2
    while div<no: 
        if no%div == 0:
            return False
            break
        div+=1 
    else:
        return True

result1 = find_prime(no)

result2 = find_prime(reversed_no)


if result1 == result2:
    print("EMIRP number")
else:
    print("not EMIRP number")

Output:

1)Enter no. 15
  51
  EMIRP number
2)Enter no. 14
  41
  not EMIRP number
Enter fullscreen mode Exit fullscreen mode

Perfect Number
Perfect number means sum of its divisible numbers will be equal that number.(eg-->6 is divisible by 1,2,3 and 1+2+3=6)

def find_perfect(no):
    total = 0
    div = 1
    while div<no:
        if no%div==0:
            total = total + div
        div+=1
    else:
        if total == no:
            return True
        else:
            return False


no = int(input("Enter no. "))
result = find_perfect(no)
if result == True:
    print("Perfect Number")
else:
    print("Not Perfect")

Output:

Enter no. 6
Perfect Number
Enter fullscreen mode Exit fullscreen mode

Square root:

Find square of an input number and sum of digits of that square root number.

def square(no):
    return no**2

no=int(input("Enter the number:"))
result=square(no)

def sum_of_digits(num):
        sum=0
        while num>0:
            sum=sum+num%10
            num=num//10
        return sum

if result<10:
    print(result)
else:
    final_result=sum_of_digits(result)
    if final_result<10:
        print(final_result) 
    else:
        final_result=sum_of_digits(final_result)
        print("sum_of_digits:",final_result)

Output:

Enter the number:4
7
Enter fullscreen mode Exit fullscreen mode

In above example given input number is 4,
-->square root of 4 is 4x4=16
-->sum of digits of that square number 1+6=7.

Task -1 **
**Automorphic Number

Check if a number’s square ends with the same number.
Example: 5 → Automorphic (5²=25), 6 → Automorphic (6²=36), 7 → Not Automorphic.

def square(no):
    return no ** 2

def find_automorphic(no):
    while result > 0:
        rem = result % 10
        if rem == no:
            print("Automorphic number")
            break
        else:
            print("Not Automorphic number")
            break

no = int(input("Enter the number: "))
result = square(no)
print(result)
find_automorphic(no)

Output:

1)Enter the number:5
  25
  Automorphic number
2)Enter the number:4
  16
  Not Automorphic number
Enter fullscreen mode Exit fullscreen mode

Top comments (0)