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

First and last digits

Python Practice › Loop Programs › Program 20

First and last digits

EasyLoop Programs · 20 of 90

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

Input
74351
Output
First digit: 7
Last digit: 1

Approach

  1. The last digit is n % 10; remove digits until one is left for the first digit.

Solution

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

Line by line

Lines 4-5

Keep dropping the last digit until a single digit remains.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Count the digits of a number

Python Practice › Loop Programs › Program 19

Count the digits of a number

EasyLoop Programs · 19 of 90

Write a Python program to count the digits of a number.

Input
908172
Output
Digits: 6

Approach

  1. Divide by 10 until the number becomes 0, counting the steps.

Solution

Python
n = 908172count = 0while n > 0:    n //= 10    count += 1print("Digits:", count)
Output
Digits: 6

Line by line

Line 4

Each //= 10 removes one digit.

Tip

len(str(abs(n))) gives the same answer for non-zero numbers.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Sum of odd numbers 1 to n

Python Practice › Loop Programs › Program 18

Sum of odd numbers 1 to n

EasyLoop Programs · 18 of 90

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

Input
n = 20
Output
Sum of odd numbers: 100

Approach

  1. Loop from 1 with a step of 2.

Solution

Python
n = 20total = 0for i in range(1, n + 1, 2):    total += iprint("Sum of odd numbers:", total)
Output
Sum of odd numbers: 100
Note

The sum of the first k odd numbers is k², so for n = 20 (ten odd numbers) the answer is 100.

Learn the concept

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

More loop programs

← All 90 Loop Programs