Experiment 3 : Conditional Statements
Que1. Check whether given number is divisible by 3 and 5 both.
Solution: num = int (input ("enter number:"))
if (num % 3 == 0 and num % 5 == 0):
print ("Number is divisible by 3 and 5")
else :
print ("not divisible by 3 and 5")
Que2.Check whether a given number is multiple of five or not.
Solution: num = int (input ("enter number:"))
if (num % 5 == 0):
print ("Number is multiple of 5")
else :
print ("Number is not multiple of 5")
Question 3: Find the greatest among two numbers. If numbers
are equal than print “numbers are equal”.
Solution. a = int (input ("enter a:"))
b = int (input ("enter b:"))
if (a < b):
print ("b is greater")
else (a == b):
print ("a and b is equal")
else:
print ("a is greater")
Question 4: Find the greatest among three numbers assuming
no two values are same.
Solution. a = int (input ("enter a:"))
b = int (input ("enter b:"))
c = int (input ("enter c:"))
if (a > b and a > c):
print ("a is greater than b and c")
elif (b > a and b > c):
print ("b is greater than a and c")
else:
print ("c is greater than a and b")
Question 5: Check whether the quadratic equation has real roots or
imaginary roots. Display the roots.
Solution. a=float(input("enter the coefficient a :"))
b=float(input("enter the coefficient b :"))
c=float(input("enter the coefficient c :"))
print("coefficient are :", "b" ,"=", b, "a","=",a, "c","=",c)
D=b**2-(4*a*c)
print(D)
if(D>0):
print("the roots are real and roots are :",-b-(D*0.5)/2*a,-b+(D*0.5)/2*a)
elif(D<0):
print("the roots are imaginary and roots are:",-b-(D*0.5)/2*a,-b+(D*0.5)/2*a)
else:
print("double root",-b/2*a)
Que6.Find whether a given year is a leap year or not
Code
y = int(input("Enter a year: "))
if (y % 4 == 0 and y % 100 != 0) or (y % 400 == 0):
print(f"{y} is a leap year.")
else:
print(f"{y} is not a leap year.")
output…
Que 7.Write a program which takes any date as input and display next
date of the calendar e.g. I/P: day=20 month=9 year=2005 O/P: day=21
month=9 year 2005
Code
d = int(input("Enter day: "))
m = int(input("Enter month: "))
y = int(input("Enter year: "))
if (y % 4 == 0 and y % 100 != 0) or (y % 400 == 0):
leap_year = True
else:
leap_year = False
if m in [1, 3, 5, 7, 8, 10, 12]:
days_in_month = 31
elif m in [4, 6, 9, 11]:
days_in_month = 30
elif m == 2:
days_in_month = 29 if leap_year else 28
if d < days_in_month:
d += 1
else:
d=1
if m < 12:
m += 1
else:
m=1
y += 1
print(f"Next Date: {d}/{m}/{y}")
output…
Que 8.Print the grade sheet of a student for the given range of cgpa.
Scan marks of five subjects and calculate the percentage.
CGPA=percentage/10 CGPA range: 0 to 3.4 -> F 3.5 to 5.0->C+ 5.1 to
6->B 6.1 to 7-> B+ 7.1 to 8-> A 8.1 to 9->A+ 9.1 to 10-> O
(Outstanding) Sample Gradesheet Name: Rohit Sharma Roll Number:
R17234512 SAPID: 50005673 Sem: 1 Course: B.Tech. CSE AI&ML
Subject name: Marks PDS: 70 Python: 80 Chemistry: 90 English: 60
Physics: 50 Percentage: 70% CGPA:7.0 Grade:
Code
name = input("Enter the student's name: ")
roll_number = input("Enter the roll number: ")
sapid = input("Enter the SAPID: ")
semester = input("Enter the semester: ")
course = input("Enter the course: ")
subject1 = int(input("Enter marks for MAths: "))
subject2 = int(input("Enter marks for Linux: "))
subject3 = int(input("Enter marks for Programming in C: "))
subject4 = int(input("Enter marks for Problem Solving: "))
subject5 = int(input("Enter marks for Environment: "))
total_marks = subject1 + subject2 + subject3 + subject4 + subject5
percentage = total_marks / 5
cgpa = percentage / 10
if cgpa <= 3.4:
grade = "F"
elif cgpa <= 5.0:
grade = "C+"
elif cgpa <= 6.0:
grade = "B"
elif cgpa <= 7.0:
grade = "B+"
elif cgpa <= 8.0:
grade = "A"
elif cgpa <= 9.0:
grade = "A+"
elif cgpa <= 10.0:
grade = "O (Outstanding)"
else:
grade = "Invalid CGPA"
print("\nSample Gradesheet")
print(f"Name: {name}")
print(f"Roll Number: {roll_number} SAPID: {sapid}")
print(f"Sem: {semester} Course: {course}")
print("Subject Marks:")
print(f"Subject 1: {subject1}")
print(f"Subject 2: {subject2}")
print(f"Subject 3: {subject3}")
print(f"Subject 4: {subject4}")
print(f"Subject 5: {subject5}")
print(f"Percentage: {percentage:.2f}%")
print(f"CGPA: {cgpa:.2f}")
print(f"Grade: {grade}")
output…