Exercises
Exercise 1: Countdown from n to 1
Write a program that reads a positive integer n from the user and prints a countdown from n to 1, each on a new line.
Countdown
n = int(input())for i in range(n, 0, -1): print(i)Exercise 2: Print characters at even positions
Write a program that reads a string from the user and prints all characters at even positions (indices 0, 2, 4, …), each on a new line.
Even Position Characters
text = input()for i in range(0, len(text), 2): print(text[i])Exercise 3: Largest digit in a number
Write a program that reads a positive integer and finds and prints the largest digit in that number.
Largest Digit
num = int(input())largest = 0while num > 0: digit = num % 10 if digit > largest: largest = digit num //= 10print(largest)Exercise 4: Sum of digits of a number
Write a program that reads a positive integer and calculates and prints the sum of all its digits.
Sum of Digits
num = int(input())sum_digits = 0while num > 0: digit = num % 10 sum_digits += digit num //= 10print(sum_digits)Exercise 5: Convert decimal to binary
Write a program that reads a positive integer and converts it to binary representation (as a string) without using built-in conversion functions.
Decimal to Binary
n = int(input())binary = ""if n == 0: binary = "0"else: while n != 0: r = n % 2 binary = str(r) + binary n = n // 2print(binary)Exercise 6: Sum of all factors of a number
Write a program that reads a positive integer n and calculates and prints the sum of all its factors (divisors).
Sum of Factors
n = int(input())sum_factors = 0for i in range(1, n + 1): if n % i == 0: sum_factors += iprint(sum_factors)Exercise 7: Check if number is palindrome
Write a program that reads a positive integer and checks if it is a palindrome (reads the same forwards and backwards). Print "Palindrome" if it is, otherwise print "Not Palindrome".
Palindrome Check
num = int(input())original = numreversed_num = 0while num > 0: digit = num % 10 reversed_num = reversed_num * 10 + digit num //= 10if original == reversed_num: print("Palindrome")else: print("Not Palindrome")Exercise 8: Calculate harmonic series
Write a program that reads a positive integer n and calculates the harmonic series . Print the result rounded to 6 decimal places.
Harmonic Series
n = int(input())harmonic = 0.0for i in range(1, n + 1): harmonic += 1.0 / iprint(round(harmonic, 6))Exercise 9: Calculate Un series
Write a program that reads a positive integer n and calculates . Print the result rounded to 6 decimal places.
Un Series
n = int(input())total = 0.0for i in range(1, n + 1): total += (i * i) / (i + 1)print(round(total, 6))Exercise 10: Equilateral triangle pattern
Write a program that reads a positive integer n and prints an equilateral triangle pattern with n rows using asterisks.
Example: If n = 4, the output should be:
* *** ************Equilateral Triangle
n = int(input())for i in range(1, n + 1): spaces = n - i stars = 2 * i - 1 print(" " * spaces + "*" * stars)Exercise 11: Diamond pattern
Write a program that reads a positive integer n and prints a diamond pattern with n rows in the upper half.
Example: If n = 4, the output should be:
* *** ************ ***** *** *Diamond Pattern
n = int(input())for i in range(1, n + 1): spaces = n - i stars = 2 * i - 1 print(" " * spaces + "*" * stars)for i in range(n - 1, 0, -1): spaces = n - i stars = 2 * i - 1 print(" " * spaces + "*" * stars)Exercise 12: FizzBuzz
Write a program that prints numbers from 1 to n (read from input), but:
- For multiples of 3, print
"Fizz"instead of the number - For multiples of 5, print
"Buzz"instead of the number - For multiples of both 3 and 5, print
"FizzBuzz"instead of the number - Otherwise, print the number
Each output should be on a new line.
FizzBuzz
n = int(input())for i in range(1, n + 1): if i % 15 == 0: print("FizzBuzz") elif i % 3 == 0: print("Fizz") elif i % 5 == 0: print("Buzz") else: print(i)Exercise 13: Find missing number
Write a program that reads an integer n, then reads n unique numbers (all between 0 and n inclusive, except one missing number). Find and print the missing number.
Example: If n = 5 and the numbers are 0, 1, 3, 4, 5, then 2 is missing.
Missing Number
n = int(input())total_sum = n * (n + 1) // 2actual_sum = 0for i in range(n): num = int(input()) actual_sum += nummissing = total_sum - actual_sumprint(missing)Exercise 14: Approximate exponential function
Write a program that reads two numbers x and n, and approximates using the Taylor series expansion:
where factorial(n) is the product of all positive integers from 1 to n. Print the result rounded to 6 decimal places.
Exponential Approximation
x = float(input())n = int(input())result = 1.0factorial = 1power = 1.0for i in range(1, n + 1): factorial *= i power *= x result += power / factorialprint(round(result, 6))Exercise 15: Caesar cipher
Write a program that reads a string message and an integer n (shift position), then shifts each letter in the message by n positions in the alphabet. Only shift letters (a-z, A-Z), leave other characters unchanged. Assume wrapping around (z shifts to a, Z shifts to A).
Caesar Cipher
message = input()n = int(input())result = ""for char in message: if 'a' <= char <= 'z': shifted = ord(char) + n if shifted > ord('z'): shifted -= 26 result += chr(shifted) elif 'A' <= char <= 'Z': shifted = ord(char) + n if shifted > ord('Z'): shifted -= 26 result += chr(shifted) else: result += charprint(result)