DEV Community

Guru prasanna
Guru prasanna

Posted on

Python Day- 14 Looping-Exercises and tasks

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")
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Output:

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

Enter fullscreen mode Exit fullscreen mode

Task:2
Fibonacci Sequence

Generate the Fibonacci sequence up to a given number.
Example: Input: 10 → Output: 0, 1, 1, 2, 3, 5, 8.

def find_fibonacci(no):
    first_num = 0
    sec_num = 1
    while first_num <= no:
        print(first_num, end=" ")
        total = first_num + sec_num  
        first_num = sec_num        
        sec_num = total      

no = int(input("Enter the number: ")) 
find_fibonacci(no)
Enter fullscreen mode Exit fullscreen mode

Output:

Enter the number of required sequence: 10
0 1 1 2 3 5 8 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)