0% found this document useful (0 votes)
11 views3 pages

Lab Code1.Docx

Ok?

Uploaded by

bonheur3737
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Lab Code1.Docx

Ok?

Uploaded by

bonheur3737
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

7)Write a program that prints minimum and maximum of five numbers entered

by the user.
Sol-
s=0
l=0
for a in range(0,5):
x = int(input("Enter the number"))
if a == 0:
s=l=x
if(x<s):
s=x
if(x>l):
l=x
print("The smallest number is" , s)
print("The largest number is" , l)

9) Write a function to print the table of a given number. The number has to be
entered by the user.
Sol-
num=int(input("Enter the number to display its table:"))
print("Table")
for a in range(1,11):
print(num, "x",a,"=",(num*a))

12) Pattern program


rows = int(input("Enter number of rows: "))

for i in range(rows, 0, -1):


for j in range(1, i+1):
print(j, end=" ")

print()

13) Write a program that uses a user defined function that accepts name and
gender (as M for Male,F for Female) and prefixes Mr./Ms. based on the gender.
Sol- def prefix(name,gender):
if(gender=='M' or gender =='m'):
print("hello,Mr. ",name)
elif(gender == 'F' or gender =='f'):
print("Hello,Ms.",name)
else:
print("please enter only M or F in gender")

name=input("Enter your name")

gender=input("Enter your gender: M for Male, and F for Female")


prefix(name,gender)

14.Write a program that has a user defined function to accept the coefficients of
a quadratic equation in variables and calculates its discriminant. For example : if
the coefficients are stored in the variables a, b, c then calculate discriminant as
.Write the appropriate condition to check discriminant on positive, zero and
negative and output appropriate result.
Sol- # defining the function which will calculate discriminant
def discriminant(a ,b, c):
d= (b^2)-( 4*a*c)
return d

#asking the user to provide values of a, b and c


print("For a quadratic equation of ax^2+ bx + c=0")
a=int(input("Enter the value of a"))
b=int(input("Enter the value of b"))
c=int(input("Enter the value of c"))

#Calling the function to get the discriminant


det= discriminant(a, b, c)

#printing the result based on the discriminant value


if(det>0):
print("The quadratic equation has two real roots.")
elif (det==0):
print("The quadratic equation has one real root.")
else:
print("The quadratic equation doesn’t have any real root.")

15. Write a program that has a user defined function to accept 2 numbers as
parameters, if number 1 is less than number 2 then numbers are swapped and
returned, i.e., number 2 is returned in place of number1 and number 1 is
reformed in place of number 2, otherwise the same order is returned.
Sol- #defining a function to swap the numbers
def swapN(a,b):
if(a<b):
return b,a
else:
return a,b

#asking the user to provide two numbers


n1=int(input("Enter number1:"))
n2=int(input("Enter number2:"))

print("Returned value from function:")

#calling function to get the returned value


n1,n2= swapN(n1,n2)
print("number 1:",n1,"number 2:",n2)

You might also like