Assignment-2
1. Write a Python program to check whether a given no is positive or negative.
In [1]: a=int(input("enter a number :"))
if a%2==0:
print("a is even")
else:
print("a is odd")
a is odd
2. Write a Python program to check whether the entered is an odd number or an even number.
In [4]: a=int(input("enter a number :"))
if a>0:
print("a is positive")
else:
print("a is negative")
a is negative
3. Write a Python program to find the largest among three given numbers.
In [7]: a=int(input("enter a number :"))
b=int(input("enter a number :"))
c=int(input("enter a number :"))
if a>b and a>c:
print(a," is largest")
elif b>c and b>a:
print(b," is largest")
else:
print(c," is largest")
90 is largest
4. Write a Python program to test whether a given no is divisible by 7 or not.
In [10]: a=int(input("enter a number :"))
if a%7==0:
print(a," is divisible by 7")
else:
print(a," is not divisible by 7")
49 is divisible by 7
5. Write a Python program to test whether the given no is divisible by both 11 & 13
In [11]: a=int(input("enter a number :"))
if a%11==0 and a%13==0:
print(a," is divisible by 11 and 13")
else:
print(a," is not divisible by 11 and 13")
155 is not divisible by 11 and 13
6. Write a Python program to evaluate the following expression X = (a−b) (c−d) and give the
error message when c == d.
In [12]: a=int(input("enter a number :"))
b=int(input("enter a number :"))
c=int(input("enter a number :"))
d=int(input("enter a number :"))
if c==d:
print("error")
else:
x=(a-b)/(c-d)
print(x)
error
7. If the cost price and selling price of an item is given as input through the key- board, write a
Python program to determine whether the seller has made a profit or incurred a loss. Also,
determine how much profit he made or the loss he incurred.
In [13]: cost=int(input("enter cost price :"))
sell=int(input("enter sell price :"))
if sell>cost:
profit=sell-cost
print("profit is",profit)
elif sell<cost:
loss=cost-sell
print("loss is",loss)
else:
print("no profit no loss")
loss is 500
8. A set of linear equations with two unknown x1 & x2 is given below.
ax1 + bx2 = m
cx1 + dx2 = n
The set has unique solutions: x1 = (md−bn)/ (ad−cb)
& x2 = (na−mc)/ (ad−cb)
Provided the denominator (ad − cb) is not equal to zero.
Write a Python program that will read the values of a, b, c, d, m, n and compute the values of
x1 & x2 . An appropriate message is printed if (ad − cb) = 0.
In [14]: a=int(input("enter value of a :"))
b=int(input("enter value of b :"))
c=int(input("enter value of c :"))
d=int(input("enter value of d :"))
m=int(input("enter value of m :"))
n=int(input("enter value of n :"))
if (a*d)-(c*b)==0:
print(error)
else:
x1=((m*d)-(b*n))/((a*d)-(c*b))
x2=((n*a)-(m*c))/((a*d)-(c*b))
print("x1=",x1)
print("x2=",x2)
x1= -3.0
x2= 3.5
9. Write a Python program to check whether a given year is a leap year or not using nested
if . else.
In [17]: year=int(input("Enter a year: "))
if year%4==0:
if year%100==0:
if year%400==0:
print(year," is a leap year.")
else:
print(year," is not a leap year.")
else:
print(year," is a leap year.")
else:
print(year," is not a leap year.")
2004 is a leap year.
10. If the three sides of a triangle are entered through the keyboard, write a Python program
to check whether the triangle is isosceles, or equilateral.
In [21]: a=int(input("enter first side :"))
b=int(input("enter second side :"))
c=int(input("enter third side :"))
if(a+b>c and a+c>b and b+c>a):
if(a==b==c):
print("Equilateral triangle")
elif a==b or b==c or c==a:
print("isoscales triangle")
else:
print("not isoscales and equilateral")
else:
print("not a valid triangle")
isoscales triangle
11. Write a Python program to perform arithmetic calculation(+, -, x, /) based on user’s choice.
In [23]: num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("\nSelect operation:")
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
choice = input("Enter your choice (1/2/3/4): ")
if choice == '1':
print("Result:", num1 + num2)
elif choice == '2':
print("Result:", num1 - num2)
elif choice == '3':
print("Result:", num1 * num2)
elif choice == '4':
if num2 != 0:
print("Result:", num1 / num2)
else:
print("Division by zero is not allowed.")
else:
print("Invalid choice.")
Select operation:
1. Addition
2. Subtraction
3. Multiplication
4. Division
Result: 1.4
12. Write a Python program that asks the user to enter their marks in 3 subjects (each subject’s
full mark is 100). The program should determine the grade according to the following rules:
A: Marks 90 and above
B: Marks 80 to 89
C: Marks 70 to 79
D: Marks 60 to 69
F: Marks below 60
In [24]: s1 = int(input("Enter marks for Subject 1 : "))
s2 = int(input("Enter marks for Subject 2 : "))
s3 = int(input("Enter marks for Subject 3 : "))
total = s1 + s2 + s3
avg = total / 3
if avg >= 90:
grade = 'A'
elif avg >= 80:
grade = 'B'
elif avg >= 70:
grade = 'C'
elif avg >= 60:
grade = 'D'
else:
grade = 'F'
print("\nTotal Marks:", total)
print("Average Marks:", avg)
print("Grade:", grade)
Total Marks: 225
Average Marks: 75.0
Grade: C
13. Write a Python program that asks the user to input their age. Based on the age entered,
classify the person into one of the following categories:
Child: Age 0 to 12 years
Teenager: Age 13 to 19 years
Adult: Age 20 to 59 years
Senior Citizen: Age 60 years and above
In [27]: age = int(input("Enter your age: "))
if 0 <= age <= 12:
category = "Child"
elif 13 <= age <= 19:
category = "Teenager"
elif 20 <= age <= 59:
category = "Adult"
elif age >= 60:
category = "Senior Citizen"
else:
category="Invalid age entered"
print("Category:", category)
Category: Senior Citizen
14. Write a Python program to get the total units consumed from the user and calculate the
electricity bill for that customer based on the following slab rates:
First 100 units -> 5 per unit
Next 100 units (101–200) -> 7 per unit
Above 200 units -> 10 per unit
In [29]: units = int(input("Enter total units consumed: "))
bill = 0
if units <= 100:
bill = units * 5
elif units <= 200:
bill = (100 * 5) + ((units - 100) * 7)
else:
bill = (100 * 5) + (100 * 7) + ((units - 200) * 10)
print("Electricity Bill:", bill)
Electricity Bill: 1300
15. Write a Python program that asks the user to input their weight (in kilograms) and height
(in meters). The program should:
Calculate the BMI using the formula: BM I = weight/ height2 Classify the BMI result into
categories:
Underweight: BMI < 18.5
Normal weight: BMI 18.5 to 24.9
Overweight: BMI 25.0 to 29.9
Obese: BMI 30.0 and above
In [32]: weight = float(input("Enter your weight in kilograms: "))
height = float(input("Enter your height in meters: "))
bmi = weight / (height ** 2)
if bmi < 18.5:
category = "Underweight"
elif 18.5 <= bmi <= 24.9:
category = "Normal weight"
elif 25.0 <= bmi <= 29.9:
category = "Overweight"
else:
category = "Obese"
print("\nYour BMI is:",bmi)
print("Category:", category)
Your BMI is: 34.72222222222222
Category: Obese
Name:Santosh Dalei
Roll No:26
SIC:23BCEA27
Branch:CEN-A2