Logo

dev-resources.site

for different kinds of informations.

Python Day- 14 Looping-Exercises and tasks

Published at
12/2/2024
Categories
payilagam
python
looping
tasks
Author
guru_prasanna_01
Categories
4 categories in total
payilagam
open
python
open
looping
open
tasks
open
Author
16 person written this
guru_prasanna_01
open
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
tasks Article's
30 articles in total
Favicon
Diving into the Use of Use Cases in JIRA🌟
Favicon
Python Day- 14 Looping-Exercises and tasks
Favicon
Python - Level : 2 Tasks
Favicon
Python - Level : 1 Tasks
Favicon
Operators, Conditionals& Inputs Tasks
Favicon
Track your Google Tasks to-do list in Google Sheets with webMethods.io Integration
Favicon
Streamlining Asynchronous Tasks in Django with Django Tasks Scheduler
Favicon
Navigating the Landscape of Tasks APIs and Integration Challenges
Favicon
Task scheduler interval in the rust
Favicon
Scheduling tasks in Golang with atomicgo
Favicon
Priority and Severity of tasks and bugs
Favicon
Command Prompt - Dealing with Tasks
Favicon
Cron Jobs - Automating tasks on Linux
Favicon
Celery & Redis : exécution de tâches en différé / asynchrones
Favicon
How to Practice Root Cause Analysis in tech problems
Favicon
Prioritizing Tasks - Time Management
Favicon
How I make myself productive with Google
Favicon
VSCode tasks and parsing your custom output for problems
Favicon
Creating containers for Django apps with periodical tasks
Favicon
Top 5 Work Habits to Boost Productivity
Favicon
Calendar Heroes: Michele Wiedemer, Manager of Customer Education at Snyk
Favicon
Calendar Heroes: Rohini Pandhi, Product @ Square
Favicon
A way to not lose track of what you've done at work
Favicon
C# - The For Loop Paradox
Favicon
Plan like a Pro with Automatic Scheduling in Taskjuggler
Favicon
Brief intro on Celery
Favicon
My approach to planning personal projects, tasks, and goals with examples
Favicon
Rails Tasks: exporting database models to a CSV.
Favicon
VSCode Tasks - Specifying custom PATH
Favicon
Using a text editor for task tracking

Featured ones: