CS Practical File
CS Practical File
OUTPUT :
Q2. A student has to travel a distance of 450 km by car at a certain
average speed. Write a python script to find the total time taken to cover
the distance.
Answer:
CODE :
distance = 450
average_speed = float(input("Enter the average speed of the car in km/h: "))
time_taken = distance / average_speed
print(“The total time taken to cover”, distance, “km at an average speed of”,
average speed, “ km/h is”, time_taken, “hours.”)
OUTPUT :
Q3. Write a program to accept the percentage of a student and display
grades.
Answer:
CODE :
OUTPUT :
Q4.Write a program that reads two numbers and an arithmetic operator
and displays the results.
Answer: CODE :
num1=float(input("Enter 1st number:"))
num2=float(input("Enter 2nd number:"))
operator=input("Enter any operator (+,-,/,*):")
if operator=='+':
result=num1+num2
print(num1,operator, num2, "=", result)
elif operator=='-' :
if num1>num2:
result=num1-num2
print(num1,operator,num2,"=", result)
elif num2>num1:
result=num2-num1
print(num2,operator,num1,"=",result)
else:
print(num1,operator,num2,"= 0")
elif operator=='/' :
if num2==0:
print(num1,operator,num2,"= infinity")
else:
result=num1/num2
print(num1,operator,num2,"=",result)
elif operator=='*' :
result=num1*num2
print(num1,operator,num2,"=",result)
else:
print("invalid operator:")
OUTPUT :
Q5. Write a program to enter a number and check if it is a prime
number or not.
Answer:
CODE :
num=int(input("Enter a number :"))
for i in range(2,num//2+1):
if num%i==0:
print("It is not a prime number")
break
else:
print("It is a prime number")
OUTPUT :
OUTPUT :
Q7.Write a python program that accepts a n number from the user and
displays the square and cube of that number.
Answer:
CODE:
num=float(input("Enter the number:"))
square=num**2
cube=num**3
print("square and cube of ",num,"are", square, "and", cube, "respectively")
OUTPUT :
GST_amount=net_price - original_price
GST_percent= (GST_amount*100) / original_price
OUTPUT :
Q9.Write a program that reads a line and prints its frequency chart like,
CODE :
OUTPUT :
Q10. Write a program to accept the radius of a circle from the user and
display its area.
Answer:
CODE :
OUTPUT :
Q11. WAP that reads a string and displays the longest substring of the
given string.
Answer:
CODE :
str1=input("Enter a string:")
word=str1.split()
maxlength=0
maxword=""
for i in word:
x=len(i)
if x>maxlength and i.isalpha()==True:
print(i)
maxlength=x
maxword=i
print("The substring with maximum length is", maxword)
OUTPUT :
List = [10,51,2,18,4,31,13,5,23,64,29]
Answer:
CODE :
OUTPUT :
Q13. WAP in python to find the maximum and minimum elements in the
list entered by the user.
Answer:
CODE :
lst=[]
num=int(input('how many numbers:'))
for n in range (num):
numbers=int(input('Enter number:'))
lst.append(numbers)
print("Maximum element in the list is:",max(lst))
print("Minimum element in the list is:", min(lst))
OUTPUT :
Q14.WAP to input ‘n’ names and phone numbers to store it in a
dictionary and print the phone number of a particular name.
Answer:
CODE :
phone=dict()
i=1
n=int(input("Enter number of entries:"))
while i<=n:
a=input("Enter name :")
b=int(input("Enter phone number:"))
phone[a]=b
i=i+1
l=phone.keys()
x=input("Enter name to be searched for:"))
for i in l:
if i==x:
print(x,": phone no. is :", phone[i])
break
else:
print(x, "doesn't exist")
OUTPUT :
Q15.WAP to input names of ‘n’ employees and their salary details like
basic salary, house rent and conveyance allowance. Calculate total salary
of each employee and display.
Answer:
CODE :
d1=dict()
i=1
n=int(input("Enter number of entries:"))
while i<=n:
Nm=input("\nEnter name of the employee:")
basic=int(input("Enter basic salary:"))
hra=int(input("Enter house rent allowance:"))
ca=int(input("Enter conveyance allowance:"))
d1[Nm]=[basic,hra,ca]
i=i+1
l=d1.keys()
print("\nName",'\t\t',"Net salary")
for i in l:
salary=0
z=d1[i]
for j in z:
salary=salary+j
print(i,'\t\t',salary)
OUTPUT :