0% found this document useful (0 votes)
3 views4 pages

Lab 05 tasks

The document outlines five tasks using while loops in Python. It includes examples for printing numbers from 1 to 5, calculating the sum of numbers from 1 to 10, printing even numbers from 1 to 20, counting down from 10 to 1, and checking if a number is prime. Each task is accompanied by code snippets and their respective outputs.

Uploaded by

rawalbaloch262
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Lab 05 tasks

The document outlines five tasks using while loops in Python. It includes examples for printing numbers from 1 to 5, calculating the sum of numbers from 1 to 10, printing even numbers from 1 to 20, counting down from 10 to 1, and checking if a number is prime. Each task is accompanied by code snippets and their respective outputs.

Uploaded by

rawalbaloch262
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab: 05 Tasks

While Loop in Python


Task 1: Print Numbers from 1 to 5 Using While Loop
Code:

num = 1

while num <= 5:

print(num)

num += 1

Output:

Task 2: Print the Sum of Numbers from 1 to 10 Using While Loop


Code:

num = 1

total_sum = 0

while num <= 10:

total_sum += num

num += 1

print("Sum from 1 to 10:", total_sum)

Output:

Sum from 1 to 10: 55


Task 3: Print Even Numbers from 1 to 20 Using While Loop
Code:

num = 1

while num <= 20:

if num % 2 == 0:

print(num)

num += 1

Output:

10

12

14

16

18

20

Task 4: Print a Countdown from 10 to 1 Using While Loop


Code:

num = 10

while num >= 1:

print(num)

num -= 1
Output:

10

Task 5: Check if a Number is Prime Using While Loop


Code:

num = 29

i=2

is_prime = True

while i <= num // 2:

if num % i == 0:

is_prime = False

break

i += 1

if is_prime:

print(num, "is a prime number")

else:

print(num, "is not a prime number")


Output:

29 is a prime number

You might also like