Multiplication table

Python Practice › Loop Programs › Program 40

Multiplication table

EasyLoop Programs · 40 of 90

Write a Python program to print the multiplication table of a number.

Input
7
Output
7 x  1 =   7
7 x  2 =  14
7 x  3 =  21
7 x  4 =  28
7 x  5 =  35
7 x  6 =  42
7 x  7 =  49
7 x  8 =  56
7 x  9 =  63
7 x 10 =  70

Approach

  1. Loop from 1 to 10 and print each product.

Solution

Python
n = 7for i in range(1, 11):    print(f"{n} x {i:2} = {n * i:3}")
Output
7 x  1 =   7
7 x  2 =  14
7 x  3 =  21
7 x  4 =  28
7 x  5 =  35
7 x  6 =  42
7 x  7 =  49
7 x  8 =  56
7 x  9 =  63
7 x 10 =  70

Line by line

Line 3

The widths :2 and :3 right-align the columns.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Fibonacci series of n terms

Python Practice › Loop Programs › Program 39

Fibonacci series of n terms

EasyLoop Programs · 39 of 90

Write a Python program to print the first n terms of the Fibonacci series.

Input
n = 12
Output
0 1 1 2 3 5 8 13 21 34 55 89 

Approach

  1. Loop n times, printing and advancing the pair.

Solution

Python
n = 12a, b = 0, 1for _ in range(n):    print(a, end=" ")    a, b = b, a + bprint()
Output
0 1 1 2 3 5 8 13 21 34 55 89 

Line by line

Line 3

The loop runs exactly n times, one term per pass.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Perfect numbers from 1 to n

Python Practice › Loop Programs › Program 38

Perfect numbers from 1 to n

MediumLoop Programs · 38 of 90

Write a Python program to print all perfect numbers from 1 to n.

Input
n = 10000
Output
[6, 28, 496, 8128]

Approach

  1. Find divisors in pairs up to √n to keep the loop fast.

Solution

Python
def divisor_sum(n):    total = 1    d = 2    while d * d <= n:        if n % d == 0:            total += d            if d != n // d:                total += n // d        d += 1    return total print([n for n in range(2, 10001) if divisor_sum(n) == n])
Output
[6, 28, 496, 8128]

Line by line

Line 4

Only divisors up to √n are tried.

Lines 5-8

Each divisor d comes with a partner n // d; both are added, except when they are the same number.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Perfect number check

Python Practice › Loop Programs › Program 37

Perfect number check

EasyLoop Programs · 37 of 90

Write a Python program to check whether a number is perfect: the sum of its factors (excluding itself) equals the number, like 28 = 1 + 2 + 4 + 7 + 14.

Input
28, 12
Output
28 is perfect
12 is not perfect (factor sum 16)

Approach

  1. Add every divisor smaller than n and compare.

Solution

Python
for n in [28, 12]:    total = 0    for d in range(1, n):        if n % d == 0:            total += d    print(n, "is perfect" if total == n else f"is not perfect (factor sum {total})")
Output
28 is perfect
12 is not perfect (factor sum 16)

Line by line

Line 3

Divisors are tested up to n – 1, so the number itself is excluded.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Armstrong numbers from 1 to n

Python Practice › Loop Programs › Program 36

Armstrong numbers from 1 to n

MediumLoop Programs · 36 of 90

Write a Python program to print all Armstrong numbers from 1 to n.

Input
n = 10000
Output
[1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474]

Approach

  1. Apply the Armstrong test to every number.

Solution

Python
def is_armstrong(n):    digits = str(n)    return n == sum(int(d) ** len(digits) for d in digits) print([n for n in range(1, 10001) if is_armstrong(n)])
Output
[1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474]

Line by line

Line 3

The sum of the powered digits is compared with the number itself.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Armstrong number with a loop

Python Practice › Loop Programs › Program 35

Armstrong number with a loop

EasyLoop Programs · 35 of 90

Write a Python program to check whether a number is an Armstrong number (153 = 1³ + 5³ + 3³) using a loop.

Input
153, 154
Output
153 is an Armstrong number
154 is not an Armstrong number

Approach

  1. Count the digits, then add each digit raised to that power.

Solution

Python
for n in [153, 154]:    power = len(str(n))    total, temp = 0, n    while temp > 0:        total += (temp % 10) ** power        temp //= 10    print(n, "is an Armstrong number" if total == n else "is not an Armstrong number")
Output
153 is an Armstrong number
154 is not an Armstrong number

Line by line

Line 2

The power equals the number of digits.

Lines 4-6

The loop peels off digits and adds their powers.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Prime factors

Python Practice › Loop Programs › Program 34

Prime factors

MediumLoop Programs · 34 of 90

Write a Python program to find all prime factors of a number.

Input
360
Output
[2, 2, 2, 3, 3, 5]
[2, 3, 5]

Approach

  1. Divide by 2, 3, 4, … as many times as possible; every successful divisor is a prime factor.

Solution

Python
n = 360factors = []d = 2while n > 1:    while n % d == 0:        factors.append(d)        n //= d    d += 1print(factors)print(sorted(set(factors)))
Output
[2, 2, 2, 3, 3, 5]
[2, 3, 5]

Line by line

Lines 5-7

The inner loop removes a factor completely (360 → 180 → 90 → 45 for d = 2).

Line 8

Composite divisors like 4 never match, because their prime parts were already removed.

Line 10

set gives the distinct prime factors.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Sum of primes from 1 to n

Python Practice › Loop Programs › Program 33

Sum of primes from 1 to n

EasyLoop Programs · 33 of 90

Write a Python program to find the sum of all prime numbers from 1 to n.

Input
n = 100
Output
Sum of primes up to 100: 1060

Approach

  1. Reuse a prime test and add the primes.

Solution

Python
def is_prime(n):    return n > 1 and all(n % d for d in range(2, int(n ** 0.5) + 1)) total = 0for n in range(1, 101):    if is_prime(n):        total += nprint("Sum of primes up to 100:", total)
Output
Sum of primes up to 100: 1060

Line by line

Line 2

all(n % d ...) is True when every remainder is non-zero, meaning no divisor exists.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Primes from 1 to n

Python Practice › Loop Programs › Program 32

Primes from 1 to n

EasyLoop Programs · 32 of 90

Write a Python program to print all prime numbers from 1 to n.

Input
n = 50
Output
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

Approach

  1. Test each number with a helper function.

Solution

Python
def is_prime(n):    if n < 2:        return False    for d in range(2, int(n ** 0.5) + 1):        if n % d == 0:            return False    return True print([n for n in range(1, 51) if is_prime(n)])
Output
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

Line by line

Line 9

The comprehension keeps the numbers that pass the test.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Prime check with a loop

Python Practice › Loop Programs › Program 31

Prime check with a loop

EasyLoop Programs · 31 of 90

Write a Python program to check whether a number is prime using a loop.

Input
97, 91
Output
97 is prime
91 is not prime, divisible by 7

Approach

  1. Use for ... else: the else runs only if no divisor was found.

Solution

Python
for n in [97, 91]:    if n < 2:        print(n, "is not prime")        continue    for d in range(2, int(n ** 0.5) + 1):        if n % d == 0:            print(n, "is not prime, divisible by", d)            break    else:        print(n, "is prime")
Output
97 is prime
91 is not prime, divisible by 7

Line by line

Lines 5-8

break stops the search at the first divisor.

Lines 9-10

The else belongs to the for; it runs only when the loop finished without break.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs