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

Sum of even numbers 1 to n

Python Practice › Loop Programs › Program 17

Sum of even numbers 1 to n

EasyLoop Programs · 17 of 90

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

Input
n = 20
Output
Sum of even numbers: 110

Approach

  1. Loop over even numbers only and add them.

Solution

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

Line by line

Line 3

The step of 2 skips odd numbers entirely.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Sum of 1 to n

Python Practice › Loop Programs › Program 16

Sum of 1 to n

EasyLoop Programs · 16 of 90

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

Input
n = 50
Output
1275 | formula: 1275

Approach

  1. Add each number to a running total.

Solution

Python
n = 50total = 0for i in range(1, n + 1):    total += iprint(total, "| formula:", n * (n + 1) // 2)
Output
1275 | formula: 1275

Line by line

Line 5

The formula n(n+1)/2 confirms the loop’s answer.

Learn the concept

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

More loop programs

← All 90 Loop Programs