0% found this document useful (0 votes)
2 views6 pages

Sample Programs On Python Loops

The document contains a series of Python loop programs that are important for exams, showcasing various functionalities such as printing numbers, calculating sums, and generating sequences. Each program includes the code and its corresponding output, covering topics like even and odd numbers, factorials, Fibonacci series, and prime number checks. It also demonstrates the use of control statements like break and continue within loops.

Uploaded by

aietivsravindra
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)
2 views6 pages

Sample Programs On Python Loops

The document contains a series of Python loop programs that are important for exams, showcasing various functionalities such as printing numbers, calculating sums, and generating sequences. Each program includes the code and its corresponding output, covering topics like even and odd numbers, factorials, Fibonacci series, and prime number checks. It also demonstrates the use of control statements like break and continue within loops.

Uploaded by

aietivsravindra
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

Python Loop Programs (Important for Exams) with Outputs

1. Print numbers 1 to 10

for i in range(1, 11):

print(i)

Output: 1 2 3 4 5 6 7 8 9 10

2. Print even numbers 1–20

for i in range(2, 21, 2):

print(i)

Output: 2 4 6 8 10 12 14 16 18 20

3. Print odd numbers 1–15

for i in range(1, 16, 2):

print(i)

Output: 1 3 5 7 9 11 13 15

4. Sum of first 10 numbers

s=0

for i in range(1, 11):

s = s+i

print("Sum =", s)

Output: Sum = 5
5. Factorial of number

n=5

fact = 1

for i in range(1, n+1):

fact =fact* i

print("Factorial =", fact)

Output:

Factorial = 120

[Link] table

n=5

for i in range(1, 11):

print(n, "x", i, "=", n*i)

Output:

5x1=5

5 x 2 = 10

... 5 x 10 = 50

7. Reverse numbers 10 to 1

for i in range(10, 0, -1):

print(i)

Output: 10 9 8 7 6 5 4 3 2 1
[Link] digits in number

n = 12345

count = 0

while n > 0:

count = count+1

n //= 10

print("Digits =", count)

Output: Digits = 5

[Link] of digits

n = 123

s=0

while n > 0:

s += n % 10

n //= 10

print("Sum =", s)

Output: Sum = 6

10. Reverse a number

n = 123

rev = 0

while n > 0:

rev = rev*10 + n%10

n //= 10

print("Reverse =", rev)


output:

11. Print square numbers 1–5

for i in range(1, 6):

print(i*i)

Output: 1 4 9 16 25 13.

12. Fibonacci series (5 terms)

a, b = 0, 1 f

or i in range(5):

print(a)

a, b = b, a+b

Output: 0 1 1 2 3

13. Check prime number

n = int(input(“Enter the Number”))

flag = True

for i in range(2, n):

if n % i == 0:

flag = False

break

if flag:

print("Prime")

else:

print("Not prime")
Output: Prime

13. Print numbers divisible by 3

for i in range(1, 21):

if i % 3 == 0:

print(i)

Output: 3 6 9 12 15 18

14. Sum of even numbers 1–10

s=0

for i in range(2, 11, 2):

s += i print("Sum =", s)

Output: Sum = 30

15. Print ASCII values A–E

for i in range(65, 70):

print(chr(i), "=", i)

Output: A = 65 B = 66 C = 67 D = 68 E = 69

15. While loop example

i=1

while i <= 5:

print(i)

i += 1

Output: 1 2 3 4 5
16. Break statement example

for i in range(1, 10):

if i == 5:

break

print(i)

Output: 1 2 3 4

17. Continue statement example

for i in range(1, 6):

if i == 3:

continue

print(i)

Output: 1 2 4 5

You might also like