Practice
Practice
Practice Paper
1. Basic Calculator
Objective: To create a simple calculator that performs basic arithmetic operations
(addition, subtraction, multiplication, and division).
Need: Calculators are essential tools for performing quick calculations. In programming,
it’s crucial to understand how arithmetic operations are performed and handled within a
program.
Example: You might need to implement this logic when building finance software,
engineering tools, or even in educational apps for students.
2. Temperature Converter
Objective: To convert temperatures between Celsius and Fahrenheit.
Need: Temperature conversion is commonly needed in scientific applications, weather
forecasting systems, and international communication where different temperature scales
are used.
Example: An app that provides weather updates globally needs to convert temperatures
from Celsius (used in most of the world) to Fahrenheit (used in the US).
1
Example: In data storage or memory optimization, palindrome patterns might be useful
for reducing redundancy.
5. Factorial Calculator
Objective: To calculate the factorial of a number, which is the product of all integers
from 1 to that number.
Need: Factorial calculations are commonly used in permutations, combinations, and
probability theory. Factorials also play a role in various mathematical algorithms.
Example: When determining the number of possible ways to arrange a set of items, you
need factorials.
7. Sum of Digits
Objective: To calculate the sum of all digits in a number.
Need: Summing digits is a simple task used in checksum algorithms, which ensure the
integrity of data (e.g., credit card numbers). It's also a common problem in coding
competitions.
Example: In credit card validation (Luhn algorithm), digit sums are used to verify
whether a credit card number is valid.
2
9. Count Vowels in a String (and consonants)
Objective: To count the number of vowels and consonants in a string.
Need: String manipulation is a core skill in many applications, including text processing,
search engines, and natural language processing (NLP).
Example: In NLP, understanding and processing vowels can be helpful in text-to-speech
programs, language recognition, and more.
3
Example: Teaching applications often generate multiplication tables to help students
practice.
4
Example: In real-world scenarios like e-commerce, you may need to remove duplicate
entries in customer data or product inventories.
Python Codes:
1. Basic Calculator
a = float(input("Enter first number: ")) # Taking first number as input and converting it
to a float
# Checking what operation was inputted and performing the corresponding arithmetic
operation
if operation == 'add':
if b != 0:
5
print(a / b) # If 'divide' and b is not 0, print the quotient
else:
2. Temperature Converter
start = int(input("Enter the start number: ")) # Taking start number as input and
converting to integer
end = int(input("Enter the end number: ")) # Taking end number as input and
converting to integer
for num in range(start, end + 1): # Looping through numbers between 'start' and 'end'
for i in range(2, int(num ** 0.5) + 1): # Check divisibility up to square root of the
number
6
if num % i == 0: # If divisible by any number other than 1 and itself, it's not
prime
is_prime = False
break
if is_prime:
else:
5. Factorial Calculator
n = int(input("Enter a number: ")) # Taking input for a number and converting to integer
n = int(input("Enter the number of terms for Fibonacci series: ")) # Taking input for the
number of terms (integer)
fib_sequence = [a, b] # Start the sequence with the first two terms
7
for _ in range(2, n): # Generate remaining terms, starting from the third
7. Sum of Digits
for digit in str(num): # Convert number to a string and loop through each
character (digit)
lst = [int(x) for x in input("Enter a list of numbers separated by spaces: ").split()] # Take
input as a space-separated string of numbers and convert to list of integers
max_value = min_value = lst[0] # Initialize max and min to the first element
of the list
for num in lst[1:]: # Loop through the list starting from the second
element
8
min_value = num # Update min_value if a smaller number is found
if char in vowels:
else:
9
print("Reversed number:", int(str(n)[::-1])) # Convert the number to a string, reverse it,
and convert it back to an integer
year = int(input("Enter a year: ")) # Taking input as an integer for the year
# Check if the year is a leap year based on the leap year rules
else:
print(f"{year} is Not a Leap Year")# If false, print that it's not a leap year
digits = [int(digit) for digit in str(num)] # Convert number to a string and then to a list of
digits (as integers)
else:
principal = float(input("Enter principal amount: ")) # Taking input for the principal
amount
rate = float(input("Enter interest rate (in %): ")) # Taking input for the interest rate
time = float(input("Enter time (in years): ")) # Taking input for the time in years
10
amount = principal * (1 + rate / 100) ** time # Calculate the final amount using
compound interest formula
n = int(input("Enter a number for the multiplication table: ")) # Taking input for a
number
a = int(input("Enter the first number: ")) # Taking input for the first number
b = int(input("Enter the second number: ")) # Taking input for the second number
x, y = a, b
11
lcm = (a * b) // gcd # Integer division to calculate the LCM
divisors = [i for i in range(1, num) if num % i == 0] # Find all divisors of the number
else:
12
18. Number Guessing Game
import random
while guess != number: # Keep looping until the correct guess is made
guess = int(input("Guess the number: ")) # Taking user input as integer for guessing
print("Too low!") # If guess is lower than the number, print "Too low!"
else:
print("You guessed it!") # If guess matches the number, print "You guessed
it!"
13
duplicates.add(num) # Add to duplicates
else:
principal = float(input("Enter principal amount: ")) # Taking input for principal amount
rate = float(input("Enter interest rate (in %): ")) # Taking input for interest rate
time = float(input("Enter time (in years): ")) # Taking input for time in years
simple_interest = (principal * rate * time) / 100 # Calculate simple interest using the
formula
14