Python Programs to Practice
1) Print: 1 1 1 1 1
Concept
Printing the same value repeatedly.
Logic
Use a loop to print 1 five times.
Algorithm
- Start
- Repeat 5 times
- Print 1
- Stop
Flowchart
┌─────────┐
│ START │
└────┬────┘
│
┌──────▼──────┐
│ count = 1 │
└──────┬──────┘
│
┌──────▼──────┐
│ count <= 5? │
└───┬────┬────┘
Yes No
│ │
│ ▼
│ ┌───────┐
│ │ END │
│ └───────┘
│
┌─────▼─────┐
│ Print 1 │
└─────┬─────┘
│
┌─────▼─────┐
│count+=1 │
└─────┬─────┘
│
└───────────► Back to Decision
Python Code
count = 1
while count <= 5:
print(1, end=" ")
count += 1
Result
2) Print: 1 2 3 4 5
Concept
Natural numbers.
Logic
Print numbers from 1 to 5.
Algorithm
- Start
- Loop from 1 to 5
- Print number
- Stop
Flowchart
START
│
▼
┌─────────┐
│ num = 1 │
└────┬────┘
│
▼
┌──────────┐
│num <= 5 ?│
└──┬────┬──┘
Yes│ │No
│ ▼
│ END
│
▼
┌──────────┐
│Print num │
└────┬─────┘
│
▼
┌──────────┐
│ num+=1 │
└────┬─────┘
│
└──────► Back to Decision
Python Code
num = 1
while num <= 5:
print(num, end=" ")
num += 1
Result
3) Print: 1 3 5 7 9
Concept
Odd numbers.
Logic
Increase by 2 each time.
Formula
Odd Number = 2n - 1
Algorithm
- Start
- Loop from 1 to 9 with step 2
- Stop
Flowchart
START
│
▼
┌─────────┐
│ num = 1 │
└────┬────┘
│
▼
┌──────────┐
│num <= 9 ?│
└──┬────┬──┘
Yes│ │No
│ ▼
│ END
│
▼
┌──────────┐
│Print num │
└────┬─────┘
│
▼
┌──────────┐
│ num+=2 │
└────┬─────┘
│
└────► Back to Decision
Python Code
num = 1
while num <= 9:
print(num, end=" ")
num += 2
Result
4) Print: 3 6 9 12 15
Concept
Multiples of 3.
Formula
Multiple = 3 × n
Algorithm
- Start
- Multiply numbers 1 to 5 by 3
- Stop
Flowchart
START
│
▼
┌─────────┐
│ num = 3 │
└────┬────┘
│
▼
┌───────────┐
│num <= 15 ?│
└──┬─────┬──┘
Yes│ │No
│ ▼
│ END
│
▼
┌──────────┐
│Print num │
└────┬─────┘
│
▼
┌──────────┐
│ num+=3 │
└────┬─────┘
│
└────► Back to Decision
Python Code
num = 3
while num <= 15:
print(num, end=" ")
num += 3
Result
4.1) Print: 15 12 9 6 3
Logic
Reverse multiples of 3.
Algorithm
Step 1: Start
Step 2: Initialize num = 15
Step 3: Repeat while num ≥ 3
Step 4: Print num
Step 5: num = num - 3
Step 6: Repeat Step 3
Step 7: Stop
Flowchart
┌─────────┐
│ START │
└────┬────┘
│
▼
┌────────────┐
│ num = 15 │
└─────┬──────┘
│
▼
┌─────────────┐
│ num >= 3 ? │
└───┬─────┬───┘
Yes No
│ │
│ ▼
│ ┌──────┐
│ │ END │
│ └──────┘
│
▼
┌─────────────┐
│ Print num │
└─────┬───────┘
│
▼
┌─────────────┐
│ num = num-3 │
└─────┬───────┘
│
└──────────► Back to Decision
Python Code
num = 15
while num >= 3:
print(num, end=" ")
num -= 3
Result
4.2) Print: 10 8 6 4 2
Concept
Reverse even numbers.
Formula
Even Number = 2n
Algorithm
Step 1: Start
Step 2: Initialize num = 10
Step 3: Repeat while num ≥ 2
Step 4: Print num
Step 5: num = num - 2
Step 6: Repeat Step 3
Step 7: Stop
Flowchart
┌─────────┐
│ START │
└────┬────┘
│
▼
┌────────────┐
│ num = 10 │
└─────┬──────┘
│
▼
┌─────────────┐
│ num >= 2 ? │
└───┬─────┬───┘
Yes No
│ │
│ ▼
│ ┌──────┐
│ │ END │
│ └──────┘
│
▼
┌─────────────┐
│ Print num │
└─────┬───────┘
│
▼
┌─────────────┐
│ num = num-2 │
└─────┬───────┘
│
└──────────► Back to Decision
Python Code
num = 10
while num >= 2:
print(num, end=" ")
num -= 2
Result
4.3) Print: 9 7 5 3 1
Concept
Reverse odd numbers.
Algorithm
Step 1: Start
Step 2: Initialize num = 9
Step 3: Repeat while num ≥ 1
Step 4: Print num
Step 5: num = num - 2
Step 6: Repeat Step 3
Step 7: Stop
Flowchart
┌─────────┐
│ START │
└────┬────┘
│
▼
┌────────────┐
│ num = 9 │
└─────┬──────┘
│
▼
┌─────────────┐
│ num >= 1 ? │
└───┬─────┬───┘
Yes No
│ │
│ ▼
│ ┌──────┐
│ │ END │
│ └──────┘
│
▼
┌─────────────┐
│ Print num │
└─────┬───────┘
│
▼
┌─────────────┐
│ num = num-2 │
└─────┬───────┘
│
└──────────► Back to Decision
Python Code
num = 9
while num >= 1:
print(num, end=" ")
num -= 2
Result
5) Multiples of 3 and 5
Concept
Numbers divisible by both 3 and 5.
Formula
n % 3 == 0 and n % 5 == 0
Algorithm
- Read limit
- Check each number
- Print if divisible by both
Flowchart
START
│
▼
┌─────────┐
│ num = 1 │
└────┬────┘
│
▼
┌────────────┐
│num <= 100 ?│
└──┬──────┬──┘
Yes│ │No
│ ▼
│ END
│
▼
┌────────────────────┐
│num%3==0 AND │
│num%5==0 ? │
└──┬─────────────┬───┘
Yes│ │No
│ │
▼ │
┌──────────┐ │
│Print num │ │
└────┬─────┘ │
│ │
└────┬──────┘
▼
┌──────────┐
│ num+=1 │
└────┬─────┘
│
└────► Back to Decision
Python Code
num = 1
while num <= 100:
if num % 3 == 0 and num % 5 == 0:
print(num, end=" ")
num += 1
Result (n=50)
6) Multiples of 3 or 5
Formula
n % 3 == 0 or n % 5 == 0
Algorithm
Step 1: Start
Step 2: Input n
Step 3: Initialize i = 1
Step 4: Repeat while i ≤ n
Step 5: If i % 3 == 0 OR i % 5 == 0, print i
Step 6: Increment i by 1
Step 7: Repeat Step 4
Step 8: Stop
Flowchart
┌─────────┐
│ START │
└────┬────┘
│
▼
┌─────────────┐
│ Input n │
└──────┬──────┘
│
▼
┌─────────────┐
│ i = 1 │
└──────┬──────┘
│
▼
┌─────────────┐
│ i <= n ? │
└───┬─────┬───┘
Yes No
│ │
│ ▼
│ ┌──────┐
│ │ END │
│ └──────┘
│
▼
┌──────────────────────────┐
│ i % 3 == 0 OR i % 5 ==0 ?│
└──────┬─────────────┬─────┘
Yes No
│ │
▼ │
┌─────────────┐ │
│ Print i │ │
└──────┬──────┘ │
│ │
└──────┬───────┘
▼
┌─────────────┐
│ i = i + 1 │
└──────┬──────┘
│
└──────────► Back to (i <= n ?)
Python Code
n = int(input("Enter limit: "))
for i in range(1, n + 1):
if i % 3 == 0 or i % 5 == 0:
print(i, end=" ")
Result (n=20)
7) Divisors of Given Number
Concept
Numbers that divide perfectly.
Formula
n % i == 0
Algorithm
- Read number
- Check 1 to n
- Print divisors
Flowchart
┌─────────┐
│ START │
└────┬────┘
│
▼
┌─────────────┐
│ Input n │
└──────┬──────┘
│
▼
┌─────────────┐
│ i = 1 │
└──────┬──────┘
│
▼
┌─────────────┐
│ i <= n ? │
└───┬─────┬───┘
Yes No
│ │
│ ▼
│ ┌──────┐
│ │ END │
│ └──────┘
│
▼
┌─────────────┐
│ n % i == 0? │
└───┬─────┬───┘
Yes No
│ │
▼ │
┌─────────┐ │
│ Print i │ │
└────┬────┘ │
│ │
└───┬───┘
▼
┌─────────┐
│ i = i+1 │
└────┬────┘
│
└────────► Back to (i <= n ?)
Python Code
n = int(input("Enter number: "))
for i in range(1, n + 1):
if n % i == 0:
print(i, end=" ")
Result
8) Count of Divisors
Concept
Count total divisors.
Algorithm
Step 1: Start
Step 2: Input n
Step 3: Initialize i = 1 and count = 0
Step 4: Repeat while i ≤ n
Step 5: If n % i == 0, increment count by 1
Step 6: Increment i by 1
Step 7: Repeat Step 4
Step 8: Print count
Step 9: Stop
Flowchart
START
│
▼
┌────────────┐
│ Input n │
└────┬───────┘
│
▼
┌─────────────────┐
│ i=1, count=0 │
└────┬────────────┘
│
▼
┌──────────┐
│ i <= n ? │
└──┬────┬──┘
Yes│ │No
│ ▼
│ ┌──────────┐
│ │PrintCount│
│ └────┬─────┘
│ │
│ END
│
▼
┌────────────┐
│ n%i == 0 ? │
└──┬─────┬───┘
Yes│ │No
│ │
▼ │
┌───────────┐
│count+=1 │
└────┬──────┘
│
▼
┌──────────┐
│ i = i+1 │
└────┬─────┘
│
└────► Back to Decision
Python Code
n = int(input("Enter number: "))
i = 1
count = 0
while i <= n:
if n % i == 0:
count += 1
i += 1
print("Count =", count)
Result
9) Prime Number
Concept
Exactly two divisors.
Formula
Count of divisors = 2
Algorithm
- Count divisors
- If count=2 → Prime
Flowchart
START
│
▼
┌────────────┐
│ Input n │
└────┬───────┘
│
▼
┌─────────────────┐
│i=1,count=0 │
└────┬────────────┘
│
▼
┌──────────┐
│ i <= n ? │
└──┬────┬──┘
Yes│ │No
│ ▼
│ ┌────────────┐
│ │count == 2 ?│
│ └──┬─────┬───┘
│ Yes│ │No
│ │ │
│ ▼ ▼
│ Prime Not Prime
│ │ │
│ └──┬──┘
│ ▼
│ END
│
▼
┌────────────┐
│ n%i == 0 ? │
└──┬─────┬───┘
Yes│ │No
│ │
▼ ▼
┌──────────┐
│count+=1 │
└────┬─────┘
▼
┌──────────┐
│ i = i+1 │
└────┬─────┘
│
└────► Back to Decision
Python Code
n = int(input("Enter number: "))
i = 1
count = 0
while i <= n:
if n % i == 0:
count += 1
i += 1
if count == 2:
print("Prime Number")
else:
print("Not Prime Number")
Result
10) Perfect Number
Concept
Sum of proper divisors equals the number.
Formula
Sum(divisors except itself) = n
Example:
6 = 1 + 2 + 3
Algorithm
Step 1: Start
Step 2: Input n
Step 3: Initialize i = 1 and total = 0
Step 4: Repeat while i < n
Step 5: If n % i == 0, add i to total
Step 6: Increment i by 1
Step 7: Repeat Step 4
Step 8: If total == n, print "Perfect Number"
Step 9: Else print "Not Perfect Number"
Step 10: Stop
Flowchart
START
│
▼
┌────────────┐
│ Input n │
└────┬───────┘
│
▼
┌─────────────────┐
│i=1,total=0 │
└────┬────────────┘
│
▼
┌──────────┐
│ i < n ? │
└──┬────┬──┘
Yes│ │No
│ ▼
│ ┌────────────┐
│ │ total==n ? │
│ └──┬─────┬───┘
│ Yes│ │No
│ ▼ ▼
│ Perfect Not Perfect
│ │ │
│ └──┬───┘
│ ▼
│ END
│
▼
┌────────────┐
│ n%i == 0 ? │
└──┬─────┬───┘
Yes│ │No
│ │
▼ ▼
┌──────────┐
│total+=i │
└────┬─────┘
▼
┌──────────┐
│ i = i+1 │
└────┬─────┘
│
└────► Back to Decision
Python Code
n = int(input("Enter number: "))
i = 1
total = 0
while i < n:
if n % i == 0:
total += i
i += 1
if total == n:
print("Perfect Number")
else:
print("Not Perfect Number")
Result
11) Print 1 3 5 7 9 2 4 6 8 10 in Single Loop
Logic
Print odds first, then evens using one loop.
Algorithm
- First five iterations → odd numbers
- Next five iterations → even numbers
Flowchart
┌─────────┐
│ START │
└────┬────┘
│
▼
┌──────────┐
│ i = 1 │
└────┬─────┘
│
▼
┌──────────┐
│ i <= 10 ?│
└──┬─────┬─┘
Yes│ │No
│ ▼
│ END
│
▼
┌──────────────────────┐
│ i <= 5 ? │
└──────┬────────────┬──┘
Yes│ │No
│ │
▼ ▼
┌─────────────────┐ ┌─────────────────┐
│ Print 2*i-1 │ │ Print 2*(i-5) │
└──────┬──────────┘ └──────┬──────────┘
│ │
└──────┬──────────────┘
▼
┌──────────┐
│ i = i+1 │
└────┬─────┘
│
└────► Back to Decision (i <= 10 ?)
Python Code
i = 1
while i <= 10:
if i <= 5:
print(2 * i - 1, end=" ")
else:
print(2 * (i - 5), end=" ")
i += 1
Result
12) Factorial of Given Number
Concept
Product of numbers from 1 to n.
Formula
n! = n × (n − 1) × (n − 2) × ... × 2 × 1
Special case:
0! = 1
Example
5! = 120
Algorithm
- fact=1
- Multiply 1 to n
- Print fact
Flowchart
┌─────────┐
│ START │
└────┬────┘
│
▼
┌────────────┐
│ Input n │
└────┬───────┘
│
▼
┌─────────────────┐
│ fact=1, i=1 │
└────┬────────────┘
│
▼
┌──────────┐
│ i <= n ? │
└──┬─────┬──┘
Yes│ │No
│ ▼
│ END
│
▼
┌──────────┐
│ fact *= i│
└────┬─────┘
│
▼
┌──────────┐
│ i = i+1 │
└────┬─────┘
│
└────► Back to Decision (i <= n ?)
Python Code
n = int(input("Enter number: "))
fact = 1
i = 1
while i <= n:
fact *= i
i += 1
print("Factorial =", fact)
Result
13) Sum of First n Numbers
Concept
Addition of natural numbers.
Formula
Sum = n(n + 1) / 2
Algorithm
- Read n
- Add 1 to n
- Print sum
Flowchart
START
│
▼
┌────────────┐
│ Input n │
└────┬───────┘
│
▼
┌────────────────┐
│i=1,total=0 │
└────┬───────────┘
│
▼
┌──────────┐
│ i <= n ? │
└──┬────┬──┘
Yes│ │No
│ ▼
│ ┌──────────┐
│ │Print Sum │
│ └────┬─────┘
│ │
│ END
│
▼
┌──────────┐
│total+=i │
└────┬─────┘
▼
┌──────────┐
│ i=i+1 │
└────┬─────┘
│
└────► Back to Decision (i <= n ?)
Python Code
n = int(input("Enter number: "))
i = 1
total = 0
while i <= n:
total += i
i += 1
print("Sum =", total)
Result
14) Print First 5 Prime Numbers
Concept
Generate prime numbers until 5 are found.
Logic
Check each number for primality.
Algorithm
Step 1: Start
Step 2: Initialize no = 2 and prime_count = 0
Step 3: Repeat while prime_count < 5
Step 4: Check whether no is prime
Step 5: If prime, print no and increment prime_count
Step 6: Increment no by 1
Step 7: Go to Step 3
Step 8: Stop
Flowchart
START
│
▼
┌────────────────────┐
│ no=2, count=0 │
└─────────┬──────────┘
│
▼
┌──────────────────┐
│ count < 5 ? │
└─────┬────────┬───┘
Yes│ │No
│ ▼
│ END
▼
┌──────────────────┐
│ find_prime(no) ? │
└─────┬────────┬───┘
Yes│ │No
▼ │
┌───────────┐ │
│ Print no │ │
└─────┬─────┘ │
▼ │
┌───────────┐ │
│count+=1 │ │
└─────┬─────┘ │
└──┬─────┘
▼
┌───────────┐
│ no = no+1 │
└─────┬─────┘
│
└────► Back to Decision (count < 5 ?)
Python Code
no = 2
prime_count = 0
while prime_count < 5:
result = find_prime(no)
if result:
print(no, end=" ")
prime_count+=1
no+=1
Result
15) Sum of First 5 Prime Numbers
Concept
Generate the first five prime numbers and add them.
Formula
2 + 3 + 5 + 7 + 11 = 28
Algorithm
- Initialize sum = 0
- Find prime numbers
- Stop after 5 primes
- Print sum
Flowchart
START
│
▼
┌────────────────────┐
│ no=2, count=0 │
└─────────┬──────────┘
│
▼
┌──────────────────┐
│ count < 5 ? │
└─────┬────────┬───┘
Yes│ │No
│ ▼
│ END
▼
┌──────────────────┐
│ find_prime(no) ? │
└─────┬────────┬───┘
Yes│ │No
▼ │
┌───────────┐ │
│ Print no │ │
└─────┬─────┘ │
▼ │
┌───────────┐ │
│count+=1 │ │
└─────┬─────┘ │
└──┬─────┘
▼
┌───────────┐
│ no = no+1 │
└─────┬─────┘
│
└────► Back to Decision (count < 5 ?)
Python Code
count = 0
total_sum = 0
num = 2
while count < 5:
i = 2
while i < num and num % i != 0:
i += 1
if i == num:
total_sum += num
count += 1
num += 1
print("Sum =", total_sum)
Result
Formula Revision Table
| Program | Formula/Idea |
|---|---|
| Same number | Repeat constant value |
| Natural numbers | i |
| Odd numbers | 2n - 1 |
| Even numbers | 2n |
| Multiples of 3 | 3 × n |
| Multiples of 5 | 5 × n |
| Divisor | n % i == 0 |
| Prime | Exactly 2 divisors |
| Perfect | Sum of proper divisors = number |
| Factorial | n! = 1×2×...×n |
| Sum of first n numbers | n(n+1)/2 |
| First 5 primes | 2, 3, 5, 7, 11 |
| Sum of first 5 primes | 28 |









Top comments (0)