Math and coding go hand in hand - numbers are everywhere in programming. Whether you’re checking if a number is prime, generating Fibonacci sequences, or spotting a palindrome, these little challenges sharpen both your math skills and your coding logic.
In this article, we’ll explore 20 beginner-friendly programming exercises based on numbers. For each one, you’ll get a quick explanation of the math concept, some sample inputs and outputs, and a ready-to-run Python solution. It’s a fun way to practice coding while learning how math ideas like perfect squares, cubes, and triangular numbers can come alive in code.
1. Sum of First N Natural Numbers
What the exercise wants:
Write a program to find the sum of the first n natural numbers. This helps you practice loops or mathematical formulas.
What is a natural number?
Natural numbers are the counting numbers starting from 1: 1, 2, 3, 4, …
Sample Input
5
Sample Output
15
Python Solution
# Read n and compute the sum of the first n natural numbers using the closed-form formula
n = int(input("Enter n: "))
# Using integer arithmetic (//) to avoid floats
total = n * (n + 1) // 2
print(total)
2. Even or Odd Checker
What the exercise wants:
Take an integer input and print whether it’s even or odd.
What is even/odd?
Even numbers are divisible by 2 (e.g., 2, 4, 6). Odd numbers leave remainder 1 when divided by 2 (e.g., 1, 3, 5).
Sample Input
7
Sample Output
7 is odd
Python Solution
# Determine if the input integer is even or odd using modulo
n = int(input("Enter a number: "))
if n % 2 == 0:
print(f"{n} is even")
else:
print(f"{n} is odd")
3. Prime Number Checker
What the exercise wants:
Write a program to check if a given number is prime.
What is a prime number?
A prime number is greater than 1 and divisible only by 1 and itself (e.g., 2, 3, 5, 7).
Sample Input
13
Sample Output
13 is prime
Python Solution
# Check primality by attempting division up to sqrt(n)
n = int(input("Enter a number: "))
if n < 2:
print(f"{n} is not prime")
else:
for d in range(2, int(n**0.5) + 1):
if n % d == 0:
print(f"{n} is not prime")
break
else:
print(f"{n} is prime")
4. Sum of Digits
What the exercise wants:
Find the sum of all digits of a number.
Why digit sums matter?
Digit sums appear in divisibility rules and various number puzzles (e.g., divisible by 3 if digit sum is divisible by 3).
Sample Input
1234
Sample Output
10
Python Solution
# Sum the digits by iterating characters and converting to integers
s = input("Enter a number: ").strip()
digit_sum = sum(int(ch) for ch in s if ch.isdigit())
print(digit_sum)
5. Palindromic Number Checker
What the exercise wants:
Check if a number reads the same backward and forward.
What is a palindromic number?
A number like 121, 1331, or 12321 that remains the same when reversed.
Sample Input
12321
Sample Output
12321 is a palindrome
Python Solution
# Compare the string with its reverse to test for palindrome
s = input("Enter a number: ").strip()
print(f"{s} is a palindrome" if s == s[::-1] else f"{s} is not a palindrome")
6. Find All Factors of a Number
What the exercise wants:
List all numbers that divide n evenly.
What are factors?
Factors are integers you can multiply to get n. For 12, factors are 1,2,3,4,6,12.
Sample Input
12
Sample Output
1 2 3 4 6 12
Python Solution
# Generate all divisors by testing from 1..n
n = int(input("Enter a number: "))
factors = [d for d in range(1, n+1) if n % d == 0]
print(" ".join(map(str, factors)))
7. Perfect Number Checker
What the exercise wants:
Check if n equals the sum of its proper divisors (excluding itself).
What is a perfect number?
Numbers like 6 (=1+2+3) and 28 (=1+2+4+7+14) are perfect numbers.
Sample Input
28
Sample Output
28 is a perfect number
Python Solution
# Sum proper divisors (exclude n) and compare to n
n = int(input("Enter a number: "))
proper = [d for d in range(1, n) if n % d == 0]
print(f"{n} is a perfect number" if sum(proper) == n else f"{n} is not a perfect number")
8. Armstrong (Narcissistic) Number Checker
What the exercise wants:
Check if n equals the sum of its digits raised to the power of the number of digits.
What is an Armstrong number?
Example: 153 = 1³ + 5³ + 3³; 9474 = 9⁴ + 4⁴ + 7⁴ + 4⁴.
Sample Input
153
Sample Output
153 is an Armstrong number
Python Solution
# Raise each digit to the power of the total number of digits and sum
s = input("Enter a number: ").strip()
p = len(s)
arm = sum(int(ch)**p for ch in s)
print(f"{s} is an Armstrong number" if arm == int(s) else f"{s} is not an Armstrong number")
9. Perfect Square Checker
What the exercise wants:
Check if a number is a perfect square.
What is a perfect square?
A number that can be written as k² (e.g., 4, 9, 16, 25).
Sample Input
49
Sample Output
49 is a perfect square
Python Solution
# Use integer square root to avoid floating-point issues
import math
n = int(input("Enter a number: "))
print(f"{n} is a perfect square" if math.isqrt(n)**2 == n else f"{n} is not a perfect square")
10. Perfect Cube Checker
What the exercise wants:
Check if a number is a perfect cube.
What is a perfect cube?
A number that can be expressed as k³ (e.g., 8, 27, 64).
Sample Input
27
Sample Output
27 is a perfect cube
Python Solution
# Approximate cube root by rounding and verify by cubing back
n = int(input("Enter a number: "))
croot = round(n ** (1/3))
print(f"{n} is a perfect cube" if croot**3 == n else f"{n} is not a perfect cube")
11. Fibonacci Sequence Generator
What the exercise wants:
Generate the first n Fibonacci numbers.
What is a Fibonacci number?
Sequence starting 0, 1 where each new term is the sum of the previous two: 0, 1, 1, 2, 3, 5, 8, …
Sample Input
7
Sample Output
0 1 1 2 3 5 8
Python Solution
# Iteratively build the Fibonacci sequence using two trackers
n = int(input("Enter n: "))
a, b = 0, 1
seq = []
for _ in range(n):
seq.append(a)
a, b = b, a + b
print(" ".join(map(str, seq)))
12. Check if a Number is in the Fibonacci Sequence
What the exercise wants:
Check if a given number belongs to the Fibonacci sequence.
Math fact:
n is Fibonacci iff (5n² + 4) or (5n² − 4) is a perfect square.
Sample Input
21
Sample Output
21 is a Fibonacci number
Python Solution
# Use the 5n^2±4 perfect-square test to check Fibonacci membership
import math
def is_square(x: int) -> bool:
return math.isqrt(x)**2 == x
n = int(input("Enter a number: "))
if is_square(5*n*n + 4) or is_square(5*n*n - 4):
print(f"{n} is a Fibonacci number")
else:
print(f"{n} is not a Fibonacci number")
13. Triangular Number Checker
What the exercise wants:
Check if n is triangular: n = k(k+1)/2 for some integer k.
What is a triangular number?
They form dot-triangles: 1, 3, 6, 10, 15, …
Sample Input
15
Sample Output
15 is a triangular number
Python Solution
# Solve k(k+1)/2 = n by checking k = floor((sqrt(8n+1)-1)/2)
import math
n = int(input("Enter a number: "))
k = int((math.isqrt(8*n + 1) - 1) // 2)
print(f"{n} is a triangular number" if k * (k + 1) // 2 == n else f"{n} is not a triangular number")
14. Rational Number (Decimal → Fraction)
What the exercise wants:
Convert a decimal to a simplified fraction p/q.
What is a rational number?
A number expressible as a ratio of integers (e.g., 0.75 = 3/4).
Sample Input
0.75
Sample Output
3/4
Python Solution
# Use Fraction to convert decimal to p/q form and simplify
from fractions import Fraction
x = float(input("Enter a decimal: "))
print(Fraction(x).limit_denominator())
15. Triangular Number Checker
What the exercise wants:
Check if a number is triangular, meaning it can form an equilateral triangle.
What is a triangular number?
Triangular numbers are sums of the first n natural numbers: 1, 3, 6, 10, 15…
Sample Input
10
Sample Output
10 is a triangular number
Python Solution
import math
n = int(input("Enter a number: "))
# Formula: if 8n+1 is a perfect square, then n is triangular
check = 8 * n + 1
if int(math.isqrt(check))**2 == check:
print(f"{n} is a triangular number")
else:
print(f"{n} is not a triangular number")
16. Fibonacci Sequence Generator
What the exercise wants:
Generate the Fibonacci sequence up to n terms.
What is the Fibonacci sequence?
It’s a sequence where each number is the sum of the previous two: 0, 1, 1, 2, 3, 5, 8…
Sample Input
7
Sample Output
0 1 1 2 3 5 8
Python Solution
n = int(input("Enter number of terms: "))
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
17. Sum of Digits
What the exercise wants:
Calculate the sum of all digits in a given number.
What does it mean?
Example: digits of 1234 are 1+2+3+4 = 10.
Sample Input
1234
Sample Output
10
Python Solution
n = input("Enter a number: ")
# Convert each character back to int and sum
s = sum(int(d) for d in n)
print(s)
18. Reverse a Number
What the exercise wants:
Reverse the digits of the given number.
What does it mean?
Example: 12345 becomes 54321.
Sample Input
12345
Sample Output
54321
Python Solution
n = input("Enter a number: ")
# Use slicing to reverse
print(n[::-1])
19. Greatest Common Divisor (GCD)
What the exercise wants:
Find the greatest number that divides two integers without remainder.
What is GCD?
The largest divisor common to both numbers. Example: gcd(12, 18) = 6.
Sample Input
12 18
Sample Output
6
Python Solution
import math
a, b = map(int, input("Enter two numbers: ").split())
# Use math.gcd for efficiency
print(math.gcd(a, b))
20. Least Common Multiple (LCM)
What the exercise wants:
Find the smallest number that is divisible by both given integers.
What is LCM?
The least common multiple of two numbers. Example: lcm(4, 6) = 12.
Sample Input
4 6
Sample Output
12
Python Solution
import math
a, b = map(int, input("Enter two numbers: ").split())
# Formula: lcm(a, b) = abs(a*b) // gcd(a, b)
print(abs(a*b) // math.gcd(a, b))