1.
Write a program to input salary provide HRA, DA and PF: sal>=30000 HRA 30% DA 20% PF 10% sal>=20000 HRA 20% DA 10%
PF 5% sal>=10000 HRA 10% DA 5% PF 3% Otherwise no HRA, DA and PF. Also, find gross and net salary.
# Function to calculate and display HRA, DA, PF, Gross, and Net Salary
def calculate_salary(salary):
if salary >= 30000:
hra = salary * 0.30
da = salary * 0.20
pf = salary * 0.10
elif salary >= 20000:
hra = salary * 0.20
da = salary * 0.10
pf = salary * 0.05
elif salary >= 10000:
hra = salary * 0.10
da = salary * 0.05
pf = salary * 0.03
else:
hra = 0
da = 0
pf = 0
gross_salary = salary + hra + da
net_salary = gross_salary - pf
print(f"HRA: {hra}")
print(f"DA: {da}")
print(f"PF: {pf}")
print(f"Gross Salary: {gross_salary}")
print(f"Net Salary: {net_salary}")
# Input salary
salary = float(input("Enter the basic salary: "))
# Calculate and display results
calculate_salary(salary)
OUTPUT:
2. Write a Menu – Driven program to input choice and compute:
Choice 1: Area of circle
Choice 2: Circumference of circle
Choice 3: Exit
import math
# Function to calculate the area of a circle
def area_of_circle(radius):
area = math.pi * radius ** 2
print(f"Area of the circle: {area:.2f}")
# Function to calculate the circumference of a circle
def circumference_of_circle(radius):
circumference = 2 * math.pi * radius
print(f"Circumference of the circle: {circumference:.2f}")
# Menu-driven program
def menu():
while True:
print("\nMenu:")
print("1. Area of circle")
print("2. Circumference of circle")
print("3. Exit")
# Get user choice
choice = int(input("Enter your choice (1-3): "))
if choice == 1:
radius = float(input("Enter the radius of the circle: "))
area_of_circle(radius)
elif choice == 2:
radius = float(input("Enter the radius of the circle: "))
circumference_of_circle(radius)
elif choice == 3:
print("Exiting the program.")
break
else:
print("Invalid choice, please try again.")
OUTPUT:
3. Write a program to check whether the user entered number is zero or negative or positive using nested if…else.
# Function to check the number
def check_number(num):
if num >= 0:
if num == 0:
print("The number is zero.")
else:
print("The number is positive.")
else:
print("The number is negative.")
# Input from the user
number = float(input("Enter a number: "))
# Check and display result
check_number(number)
OUTPUT:
4. Write a program to find the maximum number out of the given three numbers.
# Function to find the maximum of three numbers
def find_maximum(num1, num2, num3):
if num1 >= num2 and num1 >= num3:
print(f"The maximum number is: {num1}")
elif num2 >= num1 and num2 >= num3:
print(f"The maximum number is: {num2}")
else:
print(f"The maximum number is: {num3}")
# Input three numbers from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
# Find and display the maximum number
find_maximum(num1, num2, num3)
OUTPUT:
5. Write a program to input 10 numbers and find sum of positive even integers, negative odd integers and negative numbers
using while loop.
# Initialize sums
sum_positive_even = 0
sum_negative_odd = 0
sum_negative = 0
# Counter for number of inputs
count = 0
# Input 10 numbers using a while loop
while count < 10:
num = int(input(f"Enter number {count + 1}: "))
# Check if the number is positive and even
if num > 0 and num % 2 == 0:
sum_positive_even += num
# Check if the number is negative and odd
elif num < 0 and num % 2 != 0:
sum_negative_odd += num
# Check if the number is negative (for all negative numbers)
if num < 0:
sum_negative += num
# Increment the counter
count += 1
# Display the results
print(f"Sum of positive even integers: {sum_positive_even}")
print(f"Sum of negative odd integers: {sum_negative_odd}")
print(f"Sum of all negative numbers: {sum_negative}")
OUTPUT:
6. Write a program to input any number and show its factors using while loop.
def x(number):
print(f"factors of{number}are:")
i=1
while i<=number:
if number%i==0:
print(i,end=" ")
i+=1
number= int(input("enter a number:"))
if number>0:
x(number)
else:
print("enter positive number")
7. Write a program to print a multiplication table of the entered number.
# Function to print the multiplication table
def print_multiplication_table(num):
print(f"Multiplication Table of {num}:")
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")
# Input from the user
number = int(input("Enter a number to print its multiplication table: "))
# Print the multiplication table
print_multiplication_table(number)
OUTPUT:
8. Write a program to input any number and print its factorial using for loop.
# Function to calculate the factorial
def factorial(num):
result = 1
for i in range(1, num + 1):
result *= i
print(f"Factorial of {num} is: {result}")
# Input from the user
number = int(input("Enter a number to find its factorial: "))
# Calculate and print the factorial
if number < 0:
print("Factorial is not defined for negative numbers.")
else:
factorial(number)
OUTPUT:
9. Write a program to find the smallest number out of the given five numbers using for loop.
# Function to find the smallest number
def find_smallest():
smallest = None
# Loop to take 5 inputs from the user
for i in range(1, 6):
num = float(input(f"Enter number {i}: "))
# On first iteration, initialize smallest to the first number
if smallest is None or num < smallest:
smallest = num
print(f"The smallest number is: {smallest}")
# Call the function to find the smallest number
find_smallest()
OUTPUT:
10. Write a program to print the sum of following series using for loop: (1+2+3+4+…..+n)
# Function to calculate the sum of the series
def sum_of_series(n):
total_sum = 0
# Loop to calculate the sum of the series
for i in range(1, n + 1):
total_sum += i
print(f"The sum of the series 1 + 2 + 3 + ... + {n} is: {total_sum}")
# Input from the user
n = int(input("Enter the value of n: "))
# Calculate and print the sum of the series
if n < 1:
print("Please enter a positive integer.")
else:
sum_of_series(n)
OUTPUT:
11. Write a program to print Fibonacci series.
# Function to print Fibonacci series
def fibonacci_series(n):
a, b = 0, 1 # Starting values for the Fibonacci series
print("Fibonacci Series:")
for _ in range(n):
print(a, end=" ")
a, b = b, a + b # Update values for the next iteration
# Input from the user
terms = int(input("Enter the number of terms in the Fibonacci series: "))
# Validate input and print the series
if terms <= 0:
print("Please enter a positive integer.")
else:
fibonacci_series(terms)
OUTPUT:
12. Write a program to input a number and print its sum of digits using while loop
# Function to calculate the sum of digits
def sum_of_digits(number):
total_sum = 0
# Use absolute value to handle negative numbers
number = abs(number)
# Calculate sum of digits using a while loop
while number > 0:
digit = number % 10 # Get the last digit
total_sum += digit # Add it to the total sum
number //= 10 # Remove the last digit
print(f"The sum of the digits is: {total_sum}")
# Input from the user
num = int(input("Enter a number: "))
# Calculate and print the sum of the digits
sum_of_digits(num)
OUTPUT:
13. Write a program to reverse the digits of an entered number using while loop.
# Function to reverse the digits of a number
def reverse_number(number):
reversed_num = 0
# Use absolute value to handle negative numbers
number = abs(number)
# Reverse the digits using a while loop
while number > 0:
digit = number % 10 # Get the last digit
reversed_num = (reversed_num * 10) + digit # Build the reversed number
number //= 10 # Remove the last digit
return reversed_num
# Input from the user
num = int(input("Enter a number: "))
# Calculate and print the reversed number
reversed_num = reverse_number(num)
print(f"The reversed number is: {reversed_num}")
OUTPUT:
14. Write a program to check whether the entered number is Palindrome or not.
# Function to check if a number is a palindrome
def is_palindrome(number):
# Convert number to string to easily reverse it
str_num = str(abs(number)) # Use absolute value to handle negative numbers
reversed_num = str_num[::-1] # Reverse the string
# Check if the original string is the same as the reversed string
if str_num == reversed_num:
return True
else:
return False
# Input from the user
num = int(input("Enter a number: "))
# Check and print whether the number is a palindrome
if is_palindrome(num):
print(f"{num} is a palindrome.")
else:
print(f"{num} is not a palindrome.")
OUTPUT:
15. Write a program to check whether the entered number is Armstrong or not.
# Function to check if a number is an Armstrong number
def is_armstrong(number):
# Convert the number to string to easily work with its digits
str_num = str(number)
num_length = len(str_num) # Find the number of digits
total_sum = 0
# Calculate the sum of each digit raised to the power of the number of digits
for digit in str_num:
total_sum += int(digit) ** num_length
# Check if the sum is equal to the original number
return total_sum == number
# Input from the user
num = int(input("Enter a number: "))
# Check and print whether the number is an Armstrong number
if is_armstrong(num):
print(f"{num} is an Armstrong number.")
else:
print(f"{num} is not an Armstrong number.")
OUTPUT: