Loop Programs

Python programs that practice for, while loops, and nested loops with numeric and pattern problems.

25 program solutions

Back to Topics

Program Solutions

1

Print Numbers 1–100

Use a loop to print numbers from 1 to 100.

Beginner
View Solution
2

Print Even Numbers in Range

Print all even numbers between 1 and a given upper limit.

Beginner
View Solution
3

Print Odd Numbers in Range

Print all odd numbers between 1 and a given upper limit.

Beginner
View Solution
4

Sum of Digits

Compute the sum of digits of an integer using a loop.

Beginner
View Solution
5

Reverse Digits of a Number

Reverse the digits of an integer using a loop.

Beginner
View Solution
6

Calculate Factorial (Loop)

Calculate the factorial of a non-negative integer using a loop.

Beginner
View Solution
7

Fibonacci Series

Print the first N terms of the Fibonacci sequence using a loop.

Beginner
View Solution
8

Palindrome Number Check

Check whether an integer is a palindrome using digit reversal.

Beginner
View Solution
9

Count Digits in a Number

Count how many digits an integer has using a loop.

Beginner
View Solution
10

GCD of Two Numbers (Loop)

Compute the greatest common divisor (GCD) of two integers using the Euclidean algorithm.

Beginner
View Solution
11

LCM of Two Numbers (Loop)

Compute the least common multiple (LCM) of two integers using GCD.

Beginner
View Solution
12

Print Prime Numbers in Range

Print all prime numbers in a given inclusive range.

Beginner
View Solution
13

Count Prime Numbers in Range

Count how many prime numbers appear in a given inclusive range.

Beginner
View Solution
14

Check Composite Number

Check whether a number is composite (non-prime, greater than 1).

Beginner
View Solution
15

Power of 2 Checker

Check whether a positive integer is a power of 2 using a loop.

Beginner
View Solution
16

Print Number Pattern

Print a basic right-angled triangle number pattern using nested loops.

Beginner
View Solution
17

Print Star Pattern

Print a right-angled triangle star pattern using nested loops.

Beginner
View Solution
18

Armstrong Numbers in Range

Print all Armstrong numbers in a given range using loops.

Beginner
View Solution
19

Strong Numbers in Range

Print all strong numbers in a given range.

Beginner
View Solution
20

Sum of Series 1/1 + 1/2 + … + 1/N

Compute the sum of the harmonic series up to N terms.

Beginner
View Solution
21

Print Multiplication Series

Print a multiplication series for a number in a compact single-line format.

Beginner
View Solution
22

Print Pyramid Pattern

Print a centered pyramid of stars using nested loops.

Beginner
View Solution
23

Print Inverted Pyramid Pattern

Print an inverted centered pyramid of stars.

Beginner
View Solution
24

Print Floyd's Triangle

Print Floyd's triangle (consecutive numbers in a right-angled triangle).

Beginner
View Solution
25

Print Pascal's Triangle

Print Pascal's triangle up to N rows using a loop and binomial coefficients.

Intermediate
View Solution