Loop Programs
for and while. Digits, patterns, Fibonacci. You will get tired of range() and that is the point.
25 programs · open one, then the next
The matching chapter is Day 9: Loops and Iteration in the Python course. For a short check, try the Python quiz (300 questions). Every topic is listed on Python exercises.
Start with Print Numbers 1–100- 01Print Numbers 1–100BeginnerUse a loop to print numbers from 1 to 100.Open example →py# Program to print numbers from 1 to 100 for i in range(1, 101): print(i)
- 02Print Even Numbers in RangeBeginnerPrint all even numbers between 1 and a given upper limit.Open example →py# Program to print even numbers up to N n = int(input("Enter upper limit: "))
- 03Print Odd Numbers in RangeBeginnerPrint all odd numbers between 1 and a given upper limit.Open example →py# Program to print odd numbers up to N n = int(input("Enter upper limit: "))
- 04Sum of DigitsBeginnerCompute the sum of digits of an integer using a loop.Open example →py# Program to find the sum of digits of a number num = int(input("Enter an integer: "))
- 05Reverse Digits of a NumberBeginnerReverse the digits of an integer using a loop.Open example →py# Program to reverse digits of a number num = int(input("Enter an integer: "))
- 06Calculate Factorial (Loop)BeginnerCalculate the factorial of a non-negative integer using a loop.Open example →py# Program to calculate factorial using a loop n = int(input("Enter a non-negative integer: "))
- 07Fibonacci SeriesBeginnerPrint the first N terms of the Fibonacci sequence using a loop.Open example →py# Program to print Fibonacci series up to N terms n = int(input("Enter number of terms: "))
- 08Palindrome Number CheckBeginnerCheck whether an integer is a palindrome using digit reversal.Open example →py# Program to check palindrome number num = int(input("Enter an integer: "))
- 09Count Digits in a NumberBeginnerCount how many digits an integer has using a loop.Open example →py# Program to count digits in a number num = int(input("Enter an integer: "))
- 10GCD of Two Numbers (Loop)BeginnerCompute the greatest common divisor (GCD) of two integers using the Euclidean algorithm.Open example →py# Program to find GCD of two numbers using Euclidean algorithm a = int(input("Enter first integer: ")) b = int(input("Enter second integer: "))
- 11LCM of Two Numbers (Loop)BeginnerCompute the least common multiple (LCM) of two integers using GCD.Open example →py# Program to find LCM of two numbers using GCD a = int(input("Enter first integer: ")) b = int(input("Enter second integer: "))
- 12Print Prime Numbers in RangeBeginnerPrint all prime numbers in a given inclusive range.Open example →py# Program to print prime numbers in a range start = int(input("Enter start of range: ")) end = int(input("Enter end of range: "))
- 13Count Prime Numbers in RangeBeginnerCount how many prime numbers appear in a given inclusive range.Open example →py# Program to count prime numbers in a range start = int(input("Enter start of range: ")) end = int(input("Enter end of range: "))
- 14Check Composite NumberBeginnerCheck whether a number is composite (non-prime, greater than 1).Open example →py# Program to check if a number is composite n = int(input("Enter an integer: "))
- 15Power of 2 CheckerBeginnerCheck whether a positive integer is a power of 2 using a loop.Open example →py# Program to check if a number is a power of 2 n = int(input("Enter a positive integer: "))
- 16Print Number PatternBeginnerPrint a basic right-angled triangle number pattern using nested loops.Open example →py# Program to print a right-angled number triangle rows = int(input("Enter number of rows: "))
- 17Print Star PatternBeginnerPrint a right-angled triangle star pattern using nested loops.Open example →py# Program to print a right-angled star pattern rows = int(input("Enter number of rows: "))
- 18Armstrong Numbers in RangeBeginnerPrint all Armstrong numbers in a given range using loops.Open example →py# Program to print Armstrong numbers in a range start = int(input("Enter start of range: ")) end = int(input("Enter end of range: "))
- 19Strong Numbers in RangeBeginnerPrint all strong numbers in a given range.Open example →py# Program to print strong numbers in a range import math
- 20Sum of Series 1/1 + 1/2 + … + 1/NBeginnerCompute the sum of the harmonic series up to N terms.Open example →py# Program to compute sum of series 1/1 + 1/2 + ... + 1/N n = int(input("Enter a positive integer N: "))
- 21Print Multiplication SeriesBeginnerPrint a multiplication series for a number in a compact single-line format.Open example →py# Program to print multiplication series for a number num = int(input("Enter a number: ")) limit = int(input("Enter limit: "))
- 22Print Pyramid PatternBeginnerPrint a centered pyramid of stars using nested loops.Open example →py# Program to print a pyramid star pattern rows = int(input("Enter number of rows: "))
- 23Print Inverted Pyramid PatternBeginnerPrint an inverted centered pyramid of stars.Open example →py# Program to print an inverted pyramid star pattern rows = int(input("Enter number of rows: "))
- 24Print Floyd's TriangleBeginnerPrint Floyd's triangle (consecutive numbers in a right-angled triangle).Open example →py# Program to print Floyd's triangle rows = int(input("Enter number of rows: "))
- 25Print Pascal's TriangleIntermediatePrint Pascal's triangle up to N rows using a loop and binomial coefficients.Open example →py# Program to print Pascal's triangle rows = int(input("Enter number of rows: "))