Basics of Python-12

Photo by Chris Ried on Unsplash

Basics of Python-12

Day-12

While loop :

While loop, here the loop will run until the given condition is satisfied.

the basic syntax of the while loop :

while(expression):

statement (T)

Let's solve a few problems on the while loop:-

  1. Write a program to print 1 to 10 numbers using a while loop :
num = 1

while num <= 10:
    print(num)
    num += 1

output :

1
2
3
4
5
6
7
8
9
10
  1. Write a program to print odd numbers in a range using a while loop
start = 1
end = 20

while start <= end:
    if start % 2 != 0:
        print(start)
    start += 1

output :

1
3
5
7
9
11
13
15
17
19

These are the basic codes using a while loop:-

  1. Write a Python program to print all even numbers between 1 and 50 using a while loop.

  2. Write a Python program to find the factorial of a number using a while loop.

  3. Write a Python program to reverse a given number using a while loop.

  4. Write a Python program to find the sum of digits in a given number using a while loop.

  5. Write a Python program to check if a given number is a palindrome or not using a while loop.

  6. Write a Python program to find the GCD of two numbers using a while loop.

  7. Write a Python program to print the Fibonacci series up to a given number using a while loop.

  8. Write a Python program to implement a binary search algorithm using a while loop.

  9. Write a Python program to find the largest and smallest elements in a list using a while loop.

  10. Write a Python program to simulate a simple guessing game using a while loop.

In my next blog, you will get to know about python functions.

Thank you !!