Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
243 views

Python Assignment Answers

The document contains 21 coding problems and their solutions in Python. The problems cover a range of concepts including finding the maximum of two or three numbers, checking the sign of a number, determining if a number is divisible or even/odd, checking for leap years, determining if a character is a letter or digit, and classifying triangles. The solutions demonstrate how to take user input, use conditional statements like if/else, perform mathematical operations, and print outputs. Overall the document shows examples of basic Python programs for solving common computational problems.

Uploaded by

Kiran Yadav S
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
243 views

Python Assignment Answers

The document contains 21 coding problems and their solutions in Python. The problems cover a range of concepts including finding the maximum of two or three numbers, checking the sign of a number, determining if a number is divisible or even/odd, checking for leap years, determining if a character is a letter or digit, and classifying triangles. The solutions demonstrate how to take user input, use conditional statements like if/else, perform mathematical operations, and print outputs. Overall the document shows examples of basic Python programs for solving common computational problems.

Uploaded by

Kiran Yadav S
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

KIRAN YADAV S

1RN19CS067

1) Program to find maximum between two numbers.

ANS:

#1) To find maximum between two numbers

print("Enter two numbers")

n1 = int(input())

n2 = int(input())

if(n1 > n2):

print(n1," is maximum number")

else:

print(n2," is maximum number")

2) Program to find maximum between three numbers.

ANS:

#2) Program to find maximum between 3 numbers

print("Enter any 3 numbers")

n1 = int(input())

n2 = int(input())

n3 = int(input())

if((n1>n2) and (n1>n3)):

print(n1," is greater of the two numbers")

elif((n2>n1) and (n2>n3)):

print(n2," is greater of the two numbers")

else:

print(n3," is greater of the two numbers")


3) Program to check whether a number is negative, positive or zero.

ANS:

#3) program to check whether a number is negative, positive or zero.

print("Enter any valid number")

num = int(input())

if(num>=1):

print(num," is positive number....")

elif(num<=-1):

print(num," is negative number....")

else:

print(num," is zero....")

4) Program to check whether a number is divisible by 5 and 11 or not.

ANS:

#4) program to check whether a number is divisible by 5 and 11 or not.

print("Enter any number")

num = int(input())

if((num % 5 == 0) and (num%11 == 0)):

print(num," is divisible by both 5 and 11")

else:

print(num," is divisible by both 5 and 11")

5) Program to check whether a number is even or odd.

ANS:

#5) program to check whether a number is even or odd.

print("Enter any number")

num = int(input())
if(num % 2 == 0):

print(num," is an even number")

else:

print(num," is an odd number")

6) Program to check whether a year is leap year or not.

ANS:

#6) program to check whether a year is leap year or not.

print("Enter a valid year")

year = int(input())

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

print(year, "is a leap year")

else:

print(year, " is not a leap year")

7) Program to check whether a character is alphabet or not.

ANS:

#7) program to check whether a character is alphabet or not.

print("Enter a character")

ch = input()

if(((ch >= 'A') and (ch <= 'Z')) or ((ch >='a') and (ch <='z'))):

print(ch," is an alphabet")

else:

print(ch," is not an alphabet")

8) Program to input any alphabet and check whether it is vowel or consonant.

ANS:
#8) program to input any alphabet and check whether it is vowel or consonant.

print("Enter a valid alphabet")

ch = input()

if(ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u'):

print(ch," is a vowel")

else:

print(ch," is a consonent")

9) Program to input any character and check whether it is alphabet, digit or special
character.
ANS:

#9) program to input any character and check whether it is alphabet, digit or special character.

print("Enter any character")

ch = input()

if(((ch >= 'A') and (ch <= 'Z')) or ((ch >='a') and (ch <='z'))):

print(ch," is an alphabet")

elif(ch >= '1' and ch<='9'):

print(ch," is a number")

else:

print(ch," is a special character")

10) Program to check whether a character is uppercase or lowercase alphabet.

#10) program to check whether a character is uppercase or lowercase alphabet.

ANS:

print("Enter a valid alphabet")


ch = input()

if((ch >= 'A') and (ch <= 'Z')):

print(ch," is an uppercase alphabet")

if((ch >= 'a') and (ch <= 'z')):

print(ch," is lowercase alphabet")

11) Program to input week number and print week day.

ANS:

#11) program to input week number and print week day.

print("Enter a valid week number")

weekno = int(input())

if(weekno == 1):

print("Sunday")

if(weekno == 2):

print("Monday")

if(weekno == 3):

print("Tuesday")

if(weekno == 4):

print("Wednesday")

if(weekno == 5):

print("Thursday")

if(weekno == 6):

print("Friday")

if(weekno == 7):

print("Saturday")

12) Program to input month number and print number of days in that month.

ANS:
#12) program to input month number and print number of days in that month.

print("Enter a valid month number")

month_no = int(input())

if((month_no==1)or(month_no==3)or(month_no==5)or(month_no==7)or(month_no==8)or(month_no=
=10)or(month_no==12)):

print("31 days")

if((month_no==2)or(month_no==4)or(month_no==6)or(month_no==9)or(month_no==11)):

print("30 days")

14) Program to input angles of a triangle and check whether triangle is valid or not.
ANS:

#14) program to input angles of a triangle and check whether triangle is valid or not.

print("Enter valid angles for the triangle")

a1 = int(input())

a2 = int(input())

a3 = int(input())

sumOfAngles = a1 + a2 + a3

if(sumOfAngles == 180):

print("Valid Triangle...")

else:

print("Invalid Triangle...")

15) Program to input all sides of a triangle and check whether triangle is valid or not.

ANS:

#15) program to input all sides of a triangle and check whether triangle is valid or not.

print("Enter the sides of a triangle")


side1 = int(input())

side2 = int(input())

side3 = int(input())

if(((side1+side2)>side3) or ((side2+side3)>side1) or ((side1+side3)>side2)):

print("Valid Triangle...")

else:

print("Invalid Triangle...")

16) Program to check whether the triangle is equilateral, isosceles or scalene triangle.

ANS:

#16) program to check whether the triangle is equilateral, isosceles or scalene triangle.

print("Enter angles of a triangle")

a1 = int(input())

a2 = int(input())

a3 = int(input())

if(a1 == a2 == a3):

print("Equilateral Triangle")

elif(a1==a2 or a2==a3 or a1==a3):

print("Isosceles Triangle")

else:

print("Scalene Triangle")

17) Program to find all roots of a quadratic equation.

ANS:

#Program to find all roots of a quadratic equation


import cmath
a = float(input('Enter a: '))
b = float(input('Enter b: '))
c = float(input('Enter c: '))
d = (b**2) - (4*a*c)
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)
print('The solution are {0} and {1}'.format(sol1,sol2))

18. program to calculate profit or loss.


ANS:

#18) 18. program to calculate profit or loss.

print("Enter the selling price and cost price")

sprice = int(input())

cprice = int(input())

if(sprice > cprice):

print("Profit: ",sprice-cprice)

else:

print("Loss: ",cprice-sprice)

19)

Program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and
Computer. Calculate percentage and grade according to following:
Percentage >= 90%: Grade A
Percentage >= 80%: Grade B
Percentage >= 70%: Grade C
Percentage >= 60%: Grade D
Percentage >= 40%: Grade E
Percentage < 40%: Grade F
ANS:
#19) program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and

#Computer. Calculate percentage and grade

Phy = float(input())

Chem = float(input())

biology = float(input())

math = float(input())

cs = float(input())

total = Phy + Chem + biology + math + cs

average = total / 5

percentage = (total / 500) * 100

print("Marks Percentage = %.2f" %percentage)

if(percentage>=90): print("Grade A")

elif(percentage>=80 and percentage<90): print("Grade B")

elif(percentage>=70 and percentage<80): print("Grade C")

elif(percentage>=60 and percentage<70): print("Grade D")

elif(percentage>=40 and percentage<60): print("Grade E")

else: print("Grade F")

20) Program to input basic salary of an employee and calculate its Gross salary
According to following:
Basic Salary <= 10000: HRA = 20%, DA = 80%
Basic Salary <= 20000: HRA = 25%, DA = 90%
Basic Salary > 20000: HRA = 30%, DA = 95%
ANS:
#20) 20. program to input basic salary of an employee and calculate its Gross salary according to
following:

print("Enter the basic salary of an employee")

Basic_Salary = float(input())

if(Basic_Salary <=10000):

HRA=(Basic_Salary*20)/100

DA = (Basic_Salary*80)/100

elif(Basic_Salary>10000 and Basic_Salary<=20000):

HRA = (Basic_Salary*25)/100

DA = (Basic_Salary*90)/100

else:

HRA = (Basic_Salary*30)/100

DA = (Basic_Salary*95)/100

Gross_Salary = Basic_Salary + HRA + DA

print("Gross Salary: ",Gross_Salary)

21) Program to input electricity unit charges and calculate total electricity bill according to the
Given condition:
For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit
For next 100 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill
ANS:

#21) program to input electricity unit charges and calculate total electricity bill according to the given
condition:
print("Enter the number of units")

units=int(input())

bill_amt=0.0

if(units<=50):

bill_amt=units*0.50

elif(units>50 and units<=150):

bill_amt=((units-50)*0.75)+(50*0.50)

elif(units>150 and units<=250):

bill_amt=((units-150)*1.20)+(100*0.75)+(50*0.50)

else:

bill_amt=((units-250)*1.5)+(150*1.20)+(100*0.75)+(50*0.50)

total_amt=float(bill_amt)+(0.20*float(bill_amt))

print("Total Amount= ",total_amt)

THANK YOU

You might also like