DEV Community

Lakshmi Pritha Nadesan
Lakshmi Pritha Nadesan

Posted on

Day 14 - Looping

1.Find the number is prime or not
A prime number is only divisible by 1 and the number itself.

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
Enter no. 13
Prime
Enter no. 12
Not Prime
Enter fullscreen mode Exit fullscreen mode

2.Reverse a number
A reverse number refers to the number obtained by reversing the digits of a given number.

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 the number:"))
reversed_no = reverse_a_no(no)
print("Reverse Number:",reversed_no)
Enter fullscreen mode Exit fullscreen mode
Enter the number:345
Reverse Number: 543
Enter fullscreen mode Exit fullscreen mode

3.Check the reversed number is prime or not
To check if the reverse of a given number is prime, we need to:
Reverse the number.
Check if the reversed number is prime.

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)
print(result1)
result2 = find_prime(reversed_no)
print(result2)

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

Enter fullscreen mode Exit fullscreen mode
Enter no. 13
31
True
True
EMIRP number
Enter no. 23
32
True
False
not EMIRP number
Enter fullscreen mode Exit fullscreen mode

4.Find a Perfect Number:
A number is considered perfect if the sum of its divisors (excluding the number itself) equals the number.

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 Number")

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

Enter fullscreen mode Exit fullscreen mode

5.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("sum_of_digits:",result)
else:
    final_result=sum_of_digits(result)
    if final_result<10:
        print("sum_of_digits:",final_result) 
    else:
        final_result=sum_of_digits(final_result)
        print("sum_of_digits:",final_result)
Enter fullscreen mode Exit fullscreen mode
Enter the number:7
sum_of_digits: 4
Enter the number:5 
sum_of_digits: 7

Enter fullscreen mode Exit fullscreen mode

6.Harshad number:
The sum of the digits of a number and divide the number by that sum, the result is an integer.

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

no = int(input("Enter no. "))
result = sum_of_digits(no)
if no%result==0:
    print("Harshad Number")
else:
    print("Not Harshad Number")

Enter fullscreen mode Exit fullscreen mode
Enter no. 12
Harshad Number
Enter no. 13
Not Harshad Number

Enter fullscreen mode Exit fullscreen mode

7.Automorphic number
Check if a number’s square ends with the same number.

def find_square(no):
    return no**2
no=int(input("Enter the number:"))
result=find_square(no)

def find_automorphic(result):
    rem=result%10
    return rem
rem=find_automorphic(result)

if no==rem:
    print("Automorphic number")
else:
    print("Not Automorphic number")   

Enter fullscreen mode Exit fullscreen mode
Enter the number:5
Automorphic number
Enter the number:7
Not Automorphic number

Enter fullscreen mode Exit fullscreen mode

8.Find a fibonacci series:
Generate the Fibonacci sequence up to a given number.

def find_fibonacci(num):
    no1=0
    no2=1
    sum=0
    while sum<=num:
        print(sum, end=" ")
        no1 = no2
        no2 = sum 
        sum = no1 + no2

num=int(input("Enter the number: ")) 
result=find_fibonacci(num)      

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

Top comments (1)

Collapse
 
phoenix_ryan profile image
Phoenix

import math
n = int(input("Enter to:"))
div = 3
while div < math.sqrt(n):
if n % 2 == 0:
print("Not Prime")
break
if n % div == 0:
print("Not Prime")
break
div += 2
else:
print("Prime")