0% found this document useful (0 votes)
6 views

Practice

Uploaded by

Asif Aman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Practice

Uploaded by

Asif Aman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

ACADEMIA

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).

3. Find Prime Numbers


Objective: To find all prime numbers between two given numbers.
Need: Prime numbers are fundamental in cryptography and are used in secure
communications (like RSA encryption). Understanding prime numbers is essential in
fields like number theory and algorithms.
Example: Encryption algorithms rely heavily on prime numbers for generating secure
keys.

4. Check for Palindrome


Objective: To check if a string or number is a palindrome (reads the same forwards and
backwards).
Need: Palindromes are used in string processing, and recognizing patterns can be
important in fields like bioinformatics (DNA sequences), data compression, and language
processing.

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.

6. Fibonacci Series Generator


Objective: To generate a sequence where each number is the sum of the two preceding
ones.
Need: Fibonacci sequences are observed in nature (e.g., branching of trees, arrangement
of leaves). They are also used in algorithm design (dynamic programming), computer
graphics, and financial modeling.
Example: In stock market analysis, the Fibonacci retracement technique is used to predict
future price movements.

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.

8. Find Maximum/Minimum in a List


Objective: To find the largest and smallest number in a list without using built-in
functions.
Need: It teaches the basics of algorithmic thinking, especially related to searching and
comparisons. Understanding how to manually find maximum/minimum values is
fundamental for optimization problems.
Example: In sorting algorithms, or when optimizing data, you often need to find the
highest or lowest values.

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.

10. Reverse a String or Number


Objective: To reverse a given string or number.
Need: Reversing data is important in many algorithms, such as in parsing, string
manipulation, and even data encryption.
Example: Palindrome checking and cryptography often involve reversing data.

11. Leap Year Checker


Objective: To determine if a given year is a leap year.
Need: Leap year checks are necessary when dealing with calendars, scheduling systems,
and date-related calculations.
Example: In calendar applications or financial systems, understanding leap years ensures
accurate date handling and billing.

12. Armstrong Number Checker


Objective: To check if a number is an Armstrong number (narcissistic number), where
the sum of its digits raised to the power of the number of digits equals the number itself.
Need: While primarily a mathematical curiosity, Armstrong numbers can be used in
problem-solving and algorithm development, especially in coding challenges.
Example: Armstrong numbers are often used in programming puzzles and competitions.

13. Calculate Compound Interest


Objective: To calculate compound interest based on principal, rate, and time.
Need: Understanding compound interest is essential for finance, banking, and investment
calculations.
Example: Banking software uses compound interest to calculate loan repayments and
savings account growth.

14. Generate Multiplication Table


Objective: To generate a multiplication table for a given number.
Need: Multiplication tables are useful in educational software and help in understanding
basic arithmetic operations programmatically.

3
Example: Teaching applications often generate multiplication tables to help students
practice.

15. Find GCD and LCM


Objective: To find the greatest common divisor (GCD) and least common multiple
(LCM) of two numbers.
Need: GCD and LCM are fundamental in number theory and have applications in
cryptography, algorithm design, and reducing fractions.
Example: GCD is used in encryption algorithms like RSA, and LCM is useful in time
scheduling or combining periodic events.

16. Check for Perfect Number


Objective: To check if a number is perfect, meaning the sum of its divisors equals the
number.
Need: Perfect numbers are interesting in mathematics and number theory. They are also
used in certain algorithms and coding competitions.
Example: Checking perfect numbers could be a component of cryptographic functions or
for solving complex mathematical problems.

17. Sum of Even and Odd Numbers in a List


Objective: To calculate the sum of even and odd numbers from a given list.
Need: Separating and analyzing even and odd numbers is a basic task in algorithm
design, often used in sorting, data analysis, and number theory.
Example: This operation might be useful in statistics when analyzing large datasets with
even and odd values.

18. Number Guessing Game


Objective: To create a simple game where the user guesses a randomly generated
number.
Need: Simple games like this teach about user input, loops, and decision-making in
programming. They can also be expanded to learn about probability.
Example: This could be a fun coding project for beginner programmers or the foundation
of more complex games like number puzzles.

19. Find Duplicates in a List


Objective: To find duplicate elements in a list.
Need: Finding duplicates is critical in data cleaning, database management, and when
optimizing storage or processing efficiency.

4
Example: In real-world scenarios like e-commerce, you may need to remove duplicate
entries in customer data or product inventories.

20. Calculate Simple Interest


Objective: To calculate simple interest based on principal, rate, and time.
Need: Simple interest calculations are useful in banking, finance, and loan management
systems. It helps in calculating how much interest needs to be paid or earned over time.
Example: Banks and financial institutions use simple interest for certain loan products or
investments.

Python Codes:
1. Basic Calculator

a = float(input("Enter first number: ")) # Taking first number as input and converting it
to a float

b = float(input("Enter second number: ")) # Taking second number as input and


converting it to a float

operation = input("Choose operation (add, subtract, multiply, divide): ") # Taking


operation as input (as a string)

# Checking what operation was inputted and performing the corresponding arithmetic
operation

if operation == 'add':

print(a + b) # If 'add', print the sum of a and b

elif operation == 'subtract':

print(a - b) # If 'subtract', print the difference

elif operation == 'multiply':

print(a * b) # If 'multiply', print the product

elif operation == 'divide':

if b != 0:

5
print(a / b) # If 'divide' and b is not 0, print the quotient

else:

print("Cannot divide by zero") # Handle divide by zero error

2. Temperature Converter

celsius = float(input("Enter temperature in Celsius: ")) # Taking input for Celsius


temperature and converting to float

fahrenheit = (celsius * 9/5) + 32 # Converting Celsius to Fahrenheit using


the formula

print(f"Temperature in Fahrenheit: {fahrenheit}") # Printing the result

fahrenheit = float(input("Enter temperature in Fahrenheit: ")) # Taking input for


Fahrenheit temperature

celsius = (fahrenheit - 32) * 5/9 # Converting Fahrenheit to Celsius


using the formula

print(f"Temperature in Celsius: {celsius}") # Printing the result

3. Find Prime Numbers

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'

if num > 1: # Only consider numbers greater than 1

is_prime = True # Assume the number is prime initially

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:

print(num) # If still prime, print the number

4. Check for Palindrome

s = input("Enter a string or number: ") # Taking input as a string

if s == s[::-1]: # Checking if the string is equal to its reverse (using slicing)

print("Palindrome") # If true, print 'Palindrome'

else:

print("Not Palindrome") # If false, print 'Not Palindrome'

5. Factorial Calculator

n = int(input("Enter a number: ")) # Taking input for a number and converting to integer

result = 1 # Initialize result to 1

for i in range(1, n + 1): # Loop from 1 to n

result *= i # Multiply the result by each number in the range

print(f"Factorial of {n} is {result}") # Print the final factorial result

6. Fibonacci Series Generator

n = int(input("Enter the number of terms for Fibonacci series: ")) # Taking input for the
number of terms (integer)

a, b = 0, 1 # Initialize first two terms of Fibonacci sequence

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

a, b = b, a + b # Update a and b to the next two terms in sequence

fib_sequence.append(b) # Add the new term to the sequence

print(fib_sequence) # Print the full Fibonacci sequence

7. Sum of Digits

num = int(input("Enter a number: ")) # Taking input as an integer number

sum_digits = 0 # Initialize sum of digits to 0

for digit in str(num): # Convert number to a string and loop through each
character (digit)

sum_digits += int(digit) # Convert each character back to an integer and add to


the sum

print(f"Sum of digits: {sum_digits}") # Print the sum of digits

8. Find Maximum/Minimum in a List

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

if num > max_value:

max_value = num # Update max_value if a larger number is found

if num < min_value:

8
min_value = num # Update min_value if a smaller number is found

print("Max:", max_value) # Print the maximum value

print("Min:", min_value) # Print the minimum value

9. Count Vowels and Consonants

s = input("Enter a string: ") # Taking input as a string

vowels = "aeiouAEIOU" # List of vowels (both uppercase and lowercase)

vowel_count = consonant_count = 0 # Initialize vowel and consonant counters

for char in s: # Loop through each character in the string

if char.isalpha(): # Check if the character is an alphabetic letter

if char in vowels:

vowel_count += 1 # If it's a vowel, increase the vowel count

else:

consonant_count += 1 # Otherwise, it's a consonant, so increase the


consonant count

print("Vowels:", vowel_count) # Print the number of vowels

print("Consonants:", consonant_count) # Print the number of consonants

10. Reverse a String or Number

s = input("Enter a string: ") # Taking input as a string

print("Reversed string:", s[::-1]) # Print the reversed string using slicing

n = int(input("Enter a number: ")) # Taking input as an integer

9
print("Reversed number:", int(str(n)[::-1])) # Convert the number to a string, reverse it,
and convert it back to an integer

11. Leap Year Checker

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

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):

print(f"{year} is a Leap Year") # If true, print that it's a leap year

else:

print(f"{year} is Not a Leap Year")# If false, print that it's not a leap year

12. Armstrong Number Checker

num = int(input("Enter a number: ")) # Taking input as an integer

digits = [int(digit) for digit in str(num)] # Convert number to a string and then to a list of
digits (as integers)

armstrong_sum = sum(digit ** len(digits) for digit in digits) # Calculate the Armstrong


sum by raising each digit to the power of the number of digits

if armstrong_sum == num: # Compare the Armstrong sum with the original


number

print(f"{num} is an Armstrong Number") # If they match, it's an Armstrong number

else:

print(f"{num} is Not an Armstrong Number") # Otherwise, it's not

13. Calculate Compound Interest

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

compound_interest = amount - principal # Calculate the compound interest by


subtracting principal from the amount

print(f"Compound Interest: {compound_interest}") # Print the compound interest

14. Generate Multiplication Table

n = int(input("Enter a number for the multiplication table: ")) # Taking input for a
number

for i in range(1, 11): # Loop from 1 to 10 to generate the table

print(f"{n} x {i} = {n * i}") # Print the multiplication result for


each value of i

15. Find GCD and LCM

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

# GCD calculation using the Euclidean algorithm

x, y = a, b

while y: # Keep going until y becomes 0

x, y = y, x % y # Update x and y with the remainder

gcd = x # The last non-zero value of x is the GCD

print("GCD:", gcd) # Print the GCD

# LCM calculation using the formula LCM(a, b) = (a * b) / GCD(a, b)

11
lcm = (a * b) // gcd # Integer division to calculate the LCM

print("LCM:", lcm) # Print the LCM

16. Check for Perfect Number

num = int(input("Enter a number: ")) # Taking input as an integer

divisors = [i for i in range(1, num) if num % i == 0] # Find all divisors of the number

if sum(divisors) == num: # Check if the sum of divisors equals the original


number

print(f"{num} is a Perfect Number") # If true, it's a perfect number

else:

print(f"{num} is Not a Perfect Number") # Otherwise, it's not a perfect number

17. Sum of Even and Odd Numbers in a List

lst = [int(x) for x in input("Enter a list of numbers separated by spaces: ").split()] #


Taking input as space-separated numbers and converting them to a list of integers

even_sum = odd_sum = 0 # Initialize sums for even and odd numbers

for num in lst: # Loop through the list

if num % 2 == 0: # Check if the number is even

even_sum += num # Add to even sum

else: # Otherwise, it's odd

odd_sum += num # Add to odd sum

print("Sum of even numbers:", even_sum) # Print the sum of even numbers

print("Sum of odd numbers:", odd_sum) # Print the sum of odd numbers

12
18. Number Guessing Game

import random

number = random.randint(1, 100) # Generate a random number between 1 and 100

guess = None # Initialize the guess variable

while guess != number: # Keep looping until the correct guess is made

guess = int(input("Guess the number: ")) # Taking user input as integer for guessing

if guess > number:

print("Too high!") # If guess is higher than the number, print "Too


high!"

elif guess < number:

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!"

19. Find Duplicates in a List

lst = [int(x) for x in input("Enter a list of numbers separated by spaces: ").split()] #


Taking input as space-separated numbers and converting them to a list of integers

seen = set() # Initialize an empty set to store seen numbers

duplicates = set() # Initialize an empty set to store duplicates

for num in lst: # Loop through the list

if num in seen: # If number is already in 'seen', it's a duplicate

13
duplicates.add(num) # Add to duplicates

else:

seen.add(num) # Otherwise, add it to 'seen'

print("Duplicates:", list(duplicates)) # Convert set of duplicates to a list and print

20. Calculate Simple Interest

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

print(f"Simple Interest: {simple_interest}") # Print the calculated simple interest

14

You might also like