Factorial with a loop

Python Practice › Loop Programs › Program 30

Factorial with a loop

EasyLoop Programs · 30 of 90

Write a Python program to calculate the factorial of a number using a loop.

Input
7
Output
7! = 5040

Approach

  1. Multiply 1 × 2 × … × n.

Solution

Python
n = 7fact = 1for i in range(1, n + 1):    fact *= iprint(f"{n}! = {fact}")
Output
7! = 5040

Learn the concept

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

More loop programs

← All 90 Loop Programs

Factors of a number

Python Practice › Loop Programs › Program 29

Factors of a number

EasyLoop Programs · 29 of 90

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

Input
36
Output
[1, 2, 3, 4, 6, 9, 12, 18, 36]

Approach

  1. Test every number from 1 to n; keep those that divide n exactly.

Solution

Python
n = 36factors = []for d in range(1, n + 1):    if n % d == 0:        factors.append(d)print(factors)
Output
[1, 2, 3, 4, 6, 9, 12, 18, 36]

Line by line

Line 4

n % d == 0 means d is a factor.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Power using a loop

Python Practice › Loop Programs › Program 28

Power using a loop

EasyLoop Programs · 28 of 90

Write a Python program to calculate base raised to a power using a for loop.

Input
base = 3, power = 4
Output
3^4 = 81 | check: 81

Approach

  1. Multiply a result by the base power times.

Solution

Python
base, power = 3, 4result = 1for _ in range(power):    result *= baseprint(f"{base}^{power} = {result}", "| check:", base ** power)
Output
3^4 = 81 | check: 81

Line by line

Line 3

The loop body runs exactly power times.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Number in words

Python Practice › Loop Programs › Program 27

Number in words

MediumLoop Programs · 27 of 90

Write a Python program that prints each digit of a number as a word.

Input
1024
Output
one zero two four

Approach

  1. Map each digit character to its word with a list.

Solution

Python
words = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]n = 1024result = []for ch in str(n):    result.append(words[int(ch)])print(" ".join(result))
Output
one zero two four

Line by line

Line 1

The list index is the digit, so words[4] is “four”.

Line 4

Looping over str(n) gives the digits from left to right.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Frequency of each digit

Python Practice › Loop Programs › Program 26

Frequency of each digit

MediumLoop Programs · 26 of 90

Write a Python program to find how often each digit appears in an integer.

Input
1223334441
Output
1 -> 2
2 -> 2
3 -> 3
4 -> 3

Approach

  1. Use a dictionary of counts, filled one digit at a time.

Solution

Python
n = 1223334441counts = {}while n > 0:    d = n % 10    counts[d] = counts.get(d, 0) + 1    n //= 10for digit in sorted(counts):    print(digit, "->", counts[digit])
Output
1 -> 2
2 -> 2
3 -> 3
4 -> 3

Line by line

Line 5

get(d, 0) returns 0 the first time a digit is seen.

Line 7

Sorting the keys prints the digits in order.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Palindrome number with a loop

Python Practice › Loop Programs › Program 25

Palindrome number with a loop

EasyLoop Programs · 25 of 90

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

Input
12321, 12345
Output
12321 palindrome
12345 not a palindrome

Approach

  1. Reverse the number with a loop and compare it with the original.

Solution

Python
for original in [12321, 12345]:    n, rev = original, 0    while n > 0:        rev = rev * 10 + n % 10        n //= 10    print(original, "palindrome" if rev == original else "not a palindrome")
Output
12321 palindrome
12345 not a palindrome

Line by line

Line 2

Work on a copy (n) so the original value is still available for the comparison.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Reverse a number

Python Practice › Loop Programs › Program 24

Reverse a number

EasyLoop Programs · 24 of 90

Write a Python program to read a number and print its reverse.

Input
98760
Output
6789

Approach

  1. Build the reverse by moving each last digit to the end of a new number.

Solution

Python
n = 98760reversed_n = 0while n > 0:    reversed_n = reversed_n * 10 + n % 10    n //= 10print(reversed_n)
Output
6789

Line by line

Line 4

Shift the result left by one place and add the next digit.

Note

The trailing 0 of 98760 becomes a leading zero, so the result prints as 6789.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Product of digits

Python Practice › Loop Programs › Program 23

Product of digits

EasyLoop Programs · 23 of 90

Write a Python program to calculate the product of the digits of a number.

Input
4352
Output
Product of digits: 120

Approach

  1. Same as the sum, but start at 1 and multiply.

Solution

Python
n = 4352product = 1while n > 0:    product *= n % 10    n //= 10print("Product of digits:", product)
Output
Product of digits: 120

Line by line

Line 2

The product starts at 1, because starting at 0 would make everything 0.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Sum of digits

Python Practice › Loop Programs › Program 22

Sum of digits

EasyLoop Programs · 22 of 90

Write a Python program to calculate the sum of the digits of a number.

Input
40721
Output
Sum of digits: 14

Approach

  1. Take the last digit, add it, remove it; repeat.

Solution

Python
n = 40721total = 0while n > 0:    total += n % 10    n //= 10print("Sum of digits:", total)
Output
Sum of digits: 14

Line by line

Line 4

n % 10 is the current last digit.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Sum of the first and last digits

Python Practice › Loop Programs › Program 21

Sum of the first and last digits

EasyLoop Programs · 21 of 90

Write a Python program to find the sum of the first and last digits of a number.

Input
74351
Output
7 + 1 = 8

Approach

  1. Find both digits as in the previous program and add them.

Solution

Python
n = 74351last = n % 10first = nwhile first >= 10:    first //= 10print(first, "+", last, "=", first + last)
Output
7 + 1 = 8

Learn the concept

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

More loop programs

← All 90 Loop Programs