Additional Question on Conditional Statements

M3-R5.1 · Chapter 4: Operators, Expressions and Python Statements · 12 min read

Q1 : Write a Python program that takes a student's marks as input and prints the corresponding grade based on the following criteria:

  • Marks 90 or above → Grade A
  • Marks 75 to 89 → Grade B
  • Marks 60 to 74 → Grade C
  • Marks 40 to 59 → Grade D
  • Marks below 40 → Grade Fail

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

if marks >= 90:

    print("Grade: A")

elif marks >= 75:

    print("Grade: B")

elif marks >= 60:

    print("Grade: C")

elif marks >= 40:

    print("Grade: D")

else:

    print("Grade: Fail")

Q2 : Write a Python program that accepts a number from the user and checks whether the number is positive, negative, or zero.

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

if num > 0:

    print("Positive number")

elif num == 0:

    print("Zero")

else:

    print("Negative number")

Q3 : Write a Python program that asks the user to enter a password. If the entered password matches the predefined password "prakash123", it should display "Access Granted." Otherwise, it should display "Access Denied."

password = input("Enter password: ")

if password == "prakash123":

    print("Access Granted.")

else:

    print("Access Denied.")

Q4 : Write a Python program that get the year from keyboard and check it is leap year of not

year = int(input("Enter a 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.")

Q5 : Write a Python program that get the age of a person from keyboard and check he /she is eligible for a driving license.

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

if age >= 18:

    print("You are eligible for a driving license.")

else:

    print("You are not eligible for a driving license.")

Q6 : Write a Python program that get an integer from keyboard and check a number is divisible by 5 and 3 or not

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

   if num % 5 == 0 and num % 3 == 0:

         print(f"{num} is divisible by both 5 and 3.")  # print(num, "is divisible by both 5 and 3.")

  else:

     print(f"{num} is not divisible by both 5 and 3.")  # print(num, "is not divisible by both 5 and 3.")

Q7 : Write a Python program  to Find the largest of among three numbers

num1 = int(input("Enter first number: "))

num2 = int(input("Enter second number: "))

num3 = int(input("Enter third number: "))

if num1> num2 and num1 > num3:

    print(f"{ num1} is the largest.")

elif num2 > num1 and num2 > num3:  or num2> num3:

    print(f"{ num2} is the largest.")

else:

    print(f"{ num3} is the largest.")

num1 = int(input("Enter first number: "))

num2 = int(input("Enter second number: "))

num3 = int(input("Enter third number: "))

max=num1

if num2>max:

    max=num2

if num3>max:

    max=num3

print (" The largest number is :", max)

Q8 : Write a Python program  to Check if a character is a vowel or consonant

char = input("Enter a character: ")

if char in ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']:

    print("The character is a vowel.")

else:

    print("The character is not a vowel.")

char = input("Enter a character: ").lower()

if char in 'aeiou':

    print(f"{char} is a vowel.")

else:

    print(f"{char} is a consonant.")

Q9 : Write a Python program  to make  a Simple Calculator (Addition, Subtraction, Multiplication, Division)

num1 = float(input("Enter first number: "))

num2 = float(input("Enter second number: "))

print("Select operation: +, -, *, /")

operation = input("Enter operation: ")

if operation == '+':

    print("Result:", num1 + num2)

elif operation == '-':

    print("Result:", num1 - num2)

elif operation == '*':

    print("Result:", num1 * num2)

elif operation == '/':

    if num2 != 0:

        print("Result:", num1 / num2)

    else:

        print("Error: Division by zero")

else:

    print("Invalid operation")

Q10 : Write a Python program  to create a simple Login System

username = input("Enter username: ")

password = input("Enter password: ")

if username == "admin" and password == "12345":

    print("Login successful!")

else:

    print("Invalid credentials!")

Q11 : Write a Python program  to check weather a triangle if valid or not

angle1 = int(input("Enter first angle: "))

angle2 = int(input("Enter second angle: "))

angle3 = int(input("Enter third angle: "))

if angle1 + angle2 + angle3 == 180:

    print("The triangle is valid")

else:

    print("The triangle is NOT valid")

Q12 : Write a Python program  to identify a day of the week by number(1 to 7)

day = int(input("Enter day number (1-7): "))

if day == 1:

    print("Monday")

elif day == 2:

    print("Tuesday")

elif day == 3:

    print("Wednesday")

elif day == 4:

    print("Thursday")

elif day == 5:

    print("Friday")

elif day == 6:

    print("Saturday")

elif day == 7:

    print("Sunday")

else:

    print("Invalid day number!")

Q13 : Write a Python program  to create Menu for following:-

Select 1- To Calculate Area of Square

                        Select 2- To Calculate the cube of a number

                        Select 3- To check Number is even or odd

print("=== Area Calculator ===")

print("1. Area of Square")

print("2. Area of Rectangle")

print("3. Area of Circle")

choice = input("Enter your choice (1-3): ")

if choice == '1':

    side = int(input("Enter the side length: "))

    print("Area of square:", side * side)

elif choice == '2':

    length = int(input("Enter length: "))

    width = int(input("Enter width: "))

    print("Area of rectangle:", length * width)

elif choice == '3':

    radius = int(input("Enter radius: "))

    print("Area of circle:", 3.14159 * radius * radius)

else:

                print("Invalid choice.")

Q14: Write a Python program  to create Menu for following –

                        Select 1- to print square of a number

                        Select 2- to print the cube of a number

                        Select 3- to check even or odd

print("=== Number Operations Menu ===")

print("1. Square a Number")

print("2. Cube a Number")

print("3. Check Even or Odd")

choice = input("Enter your choice (1-3): ")

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

if choice == '1':

    print("Square:", num ** 2)

elif choice == '2':

    print("Cube:", num ** 3)

elif choice == '3':

    if num % 2 == 0:

        print("Even Number")

    else:

        print("Odd Number")

else:

    print("Invalid choice.")

Q15: Write a Python program  to print the larger number using swap

 num1=num2=0

num1 = int(input("Enter first number: "))

num2 = int(input("Enter second number: "))

if(num1>num2):

    num1,num2=num2,num1

    print(" The larger Number is :",num2)

    print(" The smaller Number is :",num1)

else:

    print(" The larger Number is :",num2)

    print(" The smaller Number is :",num1)

Q16: Write a Python program to display Menu Calculate sum or product three number enter by   kneeboard

            num1 = int(input("Enter first number: "))

num2 = int(input("Enter second number: "))

num3 = int(input("Enter Third number: "))

print(" Select 1- To calculate sum of numbers")

print(" Select 2- To calculate Product of numbers")

choice=int(input("Enter your choice (1 or 2):"))

if (choice==1):

           sum=num1+num2+num3

           print(" Sum of Three numbers are :",sum)

else:

    product=num1*num2*num3

    print(" Sum of Three numbers are :",product)         

Q17: Write a Python program to check character is alphabetic character or not

ch=input(“Plz enter an alphabetic character:”)

If (ch>=”A” and ch<=”Z”):

            print(“the character is an upper-case letter”)

Elif(ch>=”a” and ch<=”z”):

      print(“the character is an lower-case letter”)

else:

      print(“this is not an alphabetic Character”)

Q18: Write a Python program to get three numbers and  print numbers in ascending order

num1 = int(input("Enter first number: "))

num2 = int(input("Enter second number: "))

num3 = int(input("Enter Third number: "))

if num1<num2 and num1< num3:

    if num2<num3:

        x,y,z=num1,num2,num3

    else:

        x,y,z=num1,num3,num2

elif  num2<num1 and num2< num3:

    if num1<num3:

        x,y,z=num2,num1,num3

    else:

        x,y,z=num2,num3,num1

else:

    if num1<num2:

        x,y,z=num3,num1,num2

    else:

        x,y,z=num3,num2,num1

print("number in ascending order are:", x,y,z)

 

Q19: Write a Python program to calculate the electric bill as per the following unit charge and fixed charge:-

  • First 100 units: ₹1.50 per unit with Rs.25 fixed Charge
  • Next 100 units: ₹2.50 per unit with Rs.50 fixed Charge
  • Next 100 units: ₹4.00 per unit with Rs.75 fixed Charge
  • Unit <=350 units: ₹5.00 per unit with Rs.100 fixed Charge
  • Otherwise Rs.1500 fixed Charge

units=int(input("please enter the number of units you consumed in a month"))

if(units<=100):

 payAmount=units*1.5

 fixedcharge=25.00

elif(units<=200):

 payAmount=(100*1.5)+(units-100)*2.5

 fixedcharge=50.00

elif(units<=300):

 payAmount=(100*1.5)+(200-100)*2.5+(units-200)*4

 fixedcharge=75.00

elif(units<=350):

 payAmount=(100*1.5)+(200-100)*2.5+(300-200)*4+(units-300)*5

 fixedcharge=100.00

else:

 payAmount=0

 fixedcharge=1500.00

Total=payAmount+fixedcharge

print("\nElecticity bill",Total)

Q20: Write a Python program to  get the number  of hours  worked and hourly rate form  keyboard of and employee . If the hours worked are 40 or less, the worker is paid regular rate and If the hours worked exceed 40, the extra hours are considered overtime, paid at 1.5 times the hourly rate so calculate the calculates regular pay, overtime pay, and total labor charges and The final result is displayed.

hw = int(input("Enter the number of hours worked: "))

hrate = int(input("Enter the hourly rate: "))

stdhrs = 40

otrate = 1.5             

if hw <= stdhrs:

    regpay = hw * hrate

    otpay = 0

else:

    regpay = stdhrs * hrate

    overhrs = hw - stdhrs

    otpay = overhrs * hrate * otrate

tpay = regpay + otpay

print("Regular pay: ₹",regpay)

print("Overtime pay: ₹",otpay)

print("Total labor charges: ₹",tpay)

Q21: Write a program to get a number from 1 to 9 and print in word

num = int(input("Enter a number (1-9): "))

if num == 1:

    print("One")

elif num == 2:

    print("Two")

elif num == 3:

    print("Three")

elif num == 4:

    print("Four")

elif num == 5:

    print("Five")

elif num == 6:

    print("Six")

elif num == 7:

    print("Seven")

elif num == 8:

    print("Eight")

elif num == 9:

    print("Nine")

else:

    print("Invalid input! Please enter a number between 1 and 9.")