Python while loop exercises for CBSE Computer Science

 

1- Python program to print First 10 Even numbers using while loop

num = 2
while(num<=20):
    print(num)
    num = num + 2

 

Output:

2
4
6
8
10
12
14
16
18
20

 

2- Python program to print First 10 Odd numbers using while loop

num = 1
while(num<=20):
    print(num)
    num = num + 2

Output:

1
3
5
7
9
11
13
15
17
19

 

3- Python program to print First 10 Natural numbers using while loop

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

Output:

1
2
3
4
5
6
7
8
9
10

 

4- Python program to print First 10 Whole numbers using while loop

num = 0
while(num<10):
    print(num)
    num = num + 1
Output:
0
1
2
3
4
5
6
7
8
9

5-Python program to print first 5 numbers and their cubes using while loop.

num = 1
print("Numbers\t Cube")
while(num<=5):
   print(num,"\t", num ** 3)
   num = num + 1

Output:

Numbers Cube
1         1
2         8
3         27
4         64
5         125

6- Python program to print a series of (10, 20, 30, 40, 50) using while loop.

num = 10
while (num <= 50) :
    print(num, end = " , \n")
    num = num +10

 

Output:
10 , 20 , 30 , 40 , 50 , 


7- Python program to print sum of first 10 Natural numbers using while loop.

num = 10
sum = 0
while num >= 1:
   sum = sum + num
   num= num - 1
print(sum)

Output:

55

8- Python program to print sum of first 10 even numbers using while loop.

num = 2
sum = 0
while num <= 20:
   sum = sum + num
   num= num + 2
print(sum)

 

Output:
110

9- Python program to print table of a given number using while loop.

i = 1
num = int(input("Enter any number  : "))
while i <= 10:
    print(num," * ",i," = ", num * i)
    i = i+1

Output:

Enter any number : 5
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

10- Python program to check weather  the inputted number is prime or not using while loop.

num1 = int(input("Enter any number : "))
k=0
if num1 == 0 or num1 == 1:
    print("Not a prime number ")
else:
   i = 2
   while(i<num1):
     if num1 % i == 0:
       k = k+1
     i = i+1
if k == 0 :
        print( num1,"is prime number")
else:
        print(num1, "is not prime number")

Output:

Enter any number : 6
6 is not prime number
>>>

 

Copywrite © 2020-2024, CBSE Python,
All Rights Reserved