Home >Backend Development >Python Tutorial >Python Day- Looping-Exercises and tasks

Python Day- Looping-Exercises and tasks

DDD
DDDOriginal
2024-12-31 13:15:12698browse

Python Day- 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")

Output:

1)Enter no. 5
  Prime
2)Enter no. 6
  Not Prime

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 #no=no//10
    return reverse

no = int(input("Enter no. ")) 
reversed_no = reverse_a_no(no) #31 71
print(reversed_no)
def find_prime(no):
    div = 2
    while div<no: 
        if no%div == 0:
            return False
            break
        div+=1 #3
    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

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

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

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

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

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

Output:

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

Task:2
Fibonacci Sequence

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

no = int(input("Enter the number of required sequence: "))

first_num,sec_num =0 ,1

while first_num < no:
    print(first_num, end=" ")
    first_num,sec_num = sec_num,first_num+sec_num

Output:

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

The above is the detailed content of Python Day- Looping-Exercises and tasks. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn