Python Unit2 Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

# Question1 : Print whether a number is even or not

num = int(input("Enter a number: "))

# Check if the number is even or odd

if num % 2 == 0:

print(f"{num} is even.")

else:

print(f"{num} is odd.")

#question2: Write a program to increment a number if it is positive.

x=10

if x>0 :

x=x+1

print(x)

# Question3 : Write a program to check if input year is leap year


year = int(input("Enter a year: "))

# Check if the year is a leap year’

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

print(f"{year} is a leap year.")

else:

print(f"{year} is not a leap year.")

#Question4 : Write a program to check that input number is odd


# Input from user

number = int(input("Enter a number: "))


# Check if the number is odd

if number % 2 != 0:

print(f"{number} is odd.")

else:

print(f"{number} is not odd.")

#Question5 : Write a program to print “Excellent” if marks is greater than 95 otherwise print
“Good”

# Input marks from user

marks = int(input("Enter your marks: "))

# Check if marks are greater than 95

if marks > 95:

print("Excellent")

else:

print("Good")

#Question6: Write a program to know profit or loss in a particular sell and purchase.

# Input purchase price and selling price from the user

purchase_price = float(input("Enter the purchase price: "))

selling_price = float(input("Enter the selling price: "))

# Calculate profit or loss

if selling_price > purchase_price:

profit = selling_price - purchase_price

print(f"Profit: {profit}")

elif selling_price < purchase_price:


loss = purchase_price - selling_price

print(f"Loss: {loss}")

else:

print("No profit, no loss.")

#Question7 : Write a program to check if user has got honours in the examination or not

# Input total marks obtained and maximum possible marks

obtained_marks = float(input("Enter the marks you obtained: "))

max_marks = float(input("Enter the maximum possible marks: "))

# Calculate the percentage

percentage = (obtained_marks / max_marks) * 100

# Check for honors

if percentage >= 75:

print("Congratulations! You have received honors.")

else:

print("You did not receive honors.")

'''Question 8: Write a program to print grade of students according to following condition.

o Excellent : Marks obtained above 95%.

o Good : Marks obtained greater than 80% and less than equal to 95%.

o Average : Marks obtained greater than 60% and less than equal to 80%.

o Below average : Marks obtained less than 60%'''


# Input total marks obtained and maximum possible marks

obtained_marks = float(input("Enter the marks you obtained: "))

max_marks = float(input("Enter the maximum possible marks: "))

# Calculate the percentage

percentage = (obtained_marks / max_marks) * 100

# Determine the grade based on percentage

if percentage > 95:

print("Grade: Excellent")

elif percentage > 80 and percentage <= 95:

print("Grade: Good")

elif percentage > 60 and percentage <= 80:

print("Grade: Average")

else:

print("Grade: Below Average")

'''Question 9Write a program to generate electricity bill on basis of following criteria. First 100
units

as 3 rupees per unit, next 100 units as 4 rupees per unit and then rest would be

calculated as 5 rupees per unit. Total units consumed would be entered by user and

program should print the total billing amount'''

# Input total units consumed

units_consumed = int(input("Enter the total units consumed: "))

# Initialize total bill amount

bill_amount = 0
# Calculate the bill based on the units consumed

if units_consumed <= 100:

bill_amount = units_consumed * 3

elif units_consumed <= 200:

bill_amount = (100 * 3) + ((units_consumed - 100) * 4)

else:

bill_amount = (100 * 3) + (100 * 4) + ((units_consumed - 200) * 5)

# Print the total bill amount

print(f"Total bill amount is: {bill_amount} rupees")

''' Question 10 :Write a program to calculate salary on the basis of following criteria if
basic<20000,

DA is 30%. If 20000<=basic<30000 Da is 40%. User would input basic and program

should print total salary.'''

# Input the basic salary

basic_salary = float(input("Enter the basic salary: "))

# Initialize DA

if basic_salary < 20000:

da = 0.30 * basic_salary # 30% DA

elif 20000 <= basic_salary < 30000:

da = 0.40 * basic_salary # 40% DA

else:

da = 0 # No DA for salaries 30,000 and above (based on the given criteria)

# Calculate total salary

total_salary = basic_salary + da
# Print the total salary

print(f"Total Salary is: {total_salary}")

''' Question 11.Write a program to take an integer number from 1 to 7 from user and print
corresponding weekday, consider 1 as Monday, also print if there is incorrect choice.'''

# Input the integer number from the user

day_number = int(input("Enter a number from 1 to 7 to get the corresponding weekday (1 for


Monday): "))

# Check the input and print the corresponding weekday

if day_number == 1:

print("Monday")

elif day_number == 2:

print("Tuesday")

elif day_number == 3:

print("Wednesday")

elif day_number == 4:

print("Thursday")

elif day_number == 5:

print("Friday")

elif day_number == 6:

print("Saturday")

elif day_number == 7:

print("Sunday")

else:

print("Incorrect choice. Please enter a number between 1 and 7.")


'''Question 12 :Write a program to take age as input from user and print “You are less than 18” if age

is less than 18, otherwise print “You are above 18”. You should also check that age could not be

less than 0. If so then print a message “Age could not be less than 0”.'''

age=int(input("enter your age"))

if age>0:

if age<18:

print("you are below 18")

else:

print("you are above 18")

else:

print("age could not be less than 0")

''''Question 13: Write a program to find the maximum of three numbers.'''

# 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: "))

# Question :Determine the maximum of the three numbers


if num1 >= num2 and num1 >= num3:

maximum = num1

elif num2 >= num1 and num2 >= num3:

maximum = num2

else:

maximum = num3
#Print the maximum number

print(f"The maximum number is: {maximum}")

''' 'Question
: Write a program to enter integer from 1 to 12 and print corresponding number of days in

the month'''

# Input the month number from the user

month_number = int(input("Enter a month number (1 to 12): "))

# Determine the number of days in the month

if month_number == 1: # January

days = 31

elif month_number == 2: # February

days = 28 # Ignoring leap years for simplicity

elif month_number == 3: # March

days = 31

elif month_number == 4: # April

days = 30

elif month_number == 5: # May

days = 31

elif month_number == 6: # June

days = 30

elif month_number == 7: # July

days = 31

elif month_number == 8: # August

days = 31

elif month_number == 9: # September


days = 30

elif month_number == 10: # October

days = 31

elif month_number == 11: # November

days = 30

elif month_number == 12: # December

days = 31

else:

print("Incorrect choice. Please enter a number between 1 and 12.")

exit()

# Print the number of days in the month


print(f"The month number {month_number} has {days} days.")

#Question Write a program to print 4 natural numbers i.e. 1,2,3,4 using while loop.
# Initialize the counter

num = 1

# Use a while loop to print natural numbers from 1 to 4

while num <= 4:

print(num)

num += 1 # Increment the counter

# Question :Write a program to print natural for first n numbers, where n would be entered by user.

# Input the value of n from the user

n = int(input("Enter a positive integer (n) to print the first n natural numbers: "))

# Initialize the counter


num = 1

# Print the first n natural numbers

while num <= n:

print(num)

num += 1 # Increment the counter

You might also like