Logo

dev-resources.site

for different kinds of informations.

day-12 at payilagam looping exercises

Published at
12/5/2024
Categories
for
while
Author
kuhanraja_ar_1df623b8e2e
Categories
2 categories in total
for
open
while
open
Author
24 person written this
kuhanraja_ar_1df623b8e2e
open
day-12 at payilagam looping exercises

1)Write a program to get this output:
1 2 3 4 5 5 4 3 2 1

no = 1
top = 5
direction = 1
while no>0:
    print(no,end= ' ')
    if no == top:
        print(no,end=' ')
        direction = -1
    no = no + direction

Output:

1 2 3 4 5 5 4 3 2 1
Enter fullscreen mode Exit fullscreen mode

Using Random module:

2) Guess the number game:
Program will run till we guess the number

i

mport random

system_no = random.randint(1,20)

while True:
    guess = int(input("Enter the No. "))
    if guess == system_no:
        print("Hurray!  I got the number!! ")
        break
    elif guess > system_no:
        print("Your guess is too high! ")
    else:
        print("Your guess is too Low! ")

Output:

Enter the No. 5
Your guess is too Low! 
Enter the No. 7
Your guess is too Low! 
Enter the No. 10
Your guess is too Low! 
Enter the No. 12
Hurray!  I got the number!! 

Enter fullscreen mode Exit fullscreen mode

3) Largest digit in a Number:

no = int(input("Enter no. "))
max_no = 0
while no>0:
    rem = no%10
    if rem>max_no:
        max_no = rem
    no//=10

print(max_no)

Output:

Enter no. 452
5
Enter fullscreen mode Exit fullscreen mode

So in 452, 5 is the largest digit in a number.

Tasks:

1) Find Smallest digit in a number:

no = int(input("Enter no. "))
min_no = 9
while no>0:
    rem = no%10
    if rem<min_no:
        min_no=rem
    no//=10

print(min_no)

Output:

Enter no. 452
2

Enter fullscreen mode Exit fullscreen mode

2) Find if all digits are equal

no=input("Enter the no. ")

num=1

while num <len(no):
    if no[num]==no[0]:   
        print("All digits are equal")
        break
    num+=1

else :
    print("Not equal")

Output:

1)Enter the no. 4444
  All digits are equal
2)Enter the no. 46562
Not equal

Enter fullscreen mode Exit fullscreen mode
for Article's
30 articles in total
Favicon
day - 16 at payilagam looping slicing stepping task
Favicon
day - 13 at payilagam puzzles looping
Favicon
day-15 at payilagam range,index,for
Favicon
day-12 at payilagam looping exercises
Favicon
enumerate in Python
Favicon
day -14 at payilagam exercises
Favicon
day - 9 at payilagam python looping
Favicon
Why Comfortable Clothing Matters for Developers
Favicon
zip in Python
Favicon
python session day-6 at payilagam for loop and if condition
Favicon
range in Python
Favicon
Expert Carpet Cleaning Services: Make Your Carpets Look New Again!
Favicon
Reliable Carpet Cleaning Services: Get Rid of Stains and Odors!
Favicon
The Ultimate Laptop Buying Guide for Coders in 2025
Favicon
Ready to Transform Your Career? Join Our Upcoming Online Webinars
Favicon
Test Automation for Workday Human Capital Management (HCM): Everything You Need to Know
Favicon
Coding for kids
Favicon
How To Choose The Best Automation Platform For Testing?
Favicon
FOR statement in PostgreSQL
Favicon
FOREACH and FOR statements with arrays in PostgreSQL
Favicon
What Does Local SEO For Electricians Mean For Your Electrical Company?
Favicon
Benefits and Challenges of Migrating from Salesforce Classic to Lightning
Favicon
The Benefits of Migrating Your Salesforce Apps to Lightning
Favicon
Synergex 7 Reviews 2023 Fake Supplements or Real Results?
Favicon
Top Benefits of Migrating to Salesforce Lightning
Favicon
Using for await...of to Iterate Over Synchronous and Asynchronous Arrays in JavaScript
Favicon
A Comprehensive Guide on Test Automation for Salesforce
Favicon
How Is Test Automation For Salesforce Beneficial?
Favicon
For...in Loops...demystified!
Favicon
Is a Javascript for loop giving you the what-for?

Featured ones: