Number programs
Part of the free LogicWiz Python course, Module 2: Decisions and loops. The examples and practice run in the app.
Board papers and placement tests keep asking the same six: prime, factorial, Fibonacci, GCD, Armstrong, palindrome. Underneath, they are all a loop plus % and //.
You'll learn to
- Peel off digits with % 10 and // 10
- Write prime, factorial, Fibonacci and GCD/LCM programs
- Check Armstrong numbers and number palindromes
- Use a trace table to catch off-by-one bugs
Learn
Peeling digits
n = 472
while n > 0:
d = n % 10
print(d)
n = n // 10
n % 10 is the last digit. n // 10 drops the last digit. Repeat until nothing is left. This one loop is the heart of digit sum, reverse, palindrome and Armstrong.
A trace table
Board examiners love trace tables. For the loop above with n = 472:
| Round | n before | d = n % 10 | n after = n // 10 |
|---|---|---|---|
| 1 | 472 | 2 | 47 |
| 2 | 47 | 7 | 4 |
| 3 | 4 | 4 | 0 |
After round 3, n > 0 is False and the loop stops. When your program is wrong, make this table by hand: the bad row shows the bug.
Reverse a number, check palindrome
n = 1221
rev = 0
t = n
while t > 0:
rev = rev * 10 + t % 10
t = t // 10
print(rev)
print("Palindrome" if rev == n else "Not palindrome")
rev * 10 shifts the digits left to make room, then the new last digit goes in. We work on a copy t so the original n survives for the comparison at the end.
Armstrong numbers
n = 153
k = len(str(n))
s = 0
t = n
while t > 0:
s += (t % 10) ** k
t //= 10
print(n, k, s, s == n)
An Armstrong number equals the sum of its digits, each raised to the number of digits: 1³ + 5³ + 3³ = 153. len(str(n)) counts the digits. Others: 370, 371, 407, and 9474 with 4 digits.
Factorial and Fibonacci
n = 5
f = 1
for i in range(1, n + 1):
f *= i
print("5! =", f)
a, b = 0, 1
for i in range(8):
print(a, end=" ")
a, b = b, a + b
print()
A product starts at 1, not 0. Fibonacci keeps the last two numbers; a, b = b, a + b moves both forward in one step.
Is it prime?
n = 91
is_prime = n >= 2
d = 2
while d * d <= n:
if n % d == 0:
is_prime = False
break
d += 1
print(n, "prime" if is_prime else "not prime")
A flag starts as "yes, prime" and flips on the first divisor. You only need to try divisors up to the square root (d * d <= n): if 91 = 7 × 13, the small factor 7 shows up first. 0 and 1 are not prime.
GCD by Euclid's method, then LCM
x, y = 12, 18
a, b = x, y
while b != 0:
a, b = b, a % b
print("GCD:", a)
print("LCM:", x * y // a)
Replace (a, b) with (b, a % b) until b is 0; then a is the GCD. (12, 18) → (18, 12) → (12, 6) → (6, 0), so the GCD is 6. LCM = x × y ÷ GCD = 36.
Test the edges, where off-by-one bugs hide: 0! is 1; 1 is not prime; 2 is prime; a one-digit number is a palindrome. Also: a loop that eats n (like n //= 10) leaves n as 0, so keep a copy if you need the original.
These six are fixtures in CBSE Class 11 practicals and first-year lab exams, and placement tests (TCS NQT, Infosys) ask them as warm-up coding questions. Know each by heart as an idea, not as memorised lines: digits with % 10 / // 10, a flag for prime, a product starting at 1.
In one line
n % 10 is the last digit and n // 10 drops it; a product starts at 1, a prime check needs a flag or a loop else, and you should always test 0, 1 and 2.