0% found this document useful (0 votes)
22 views19 pages

CS Practical File

Uploaded by

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

CS Practical File

Uploaded by

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

PERFORM THE FOLLOWING PRACTICALS AND WRITE

THE PYTHON CODE FOR THE SAME ALONG WITH THE


GENERAL OUTPUT

Q1. Input two numbers and display the larger/smaller number.


Answer:
CODE :
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if num1 > num2:
print("The larger number is:", num1)
print("The smaller number is:", num2)
elif num2 > num1:
print("The larger number is:", num2)
print("The smaller number is:", num1)
else:
print("Both numbers are equal.")

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 :

percentage = float(input("Enter the percentage obtained by the student: "))


if percentage >= 90:
grade = 'A'
elif percentage >= 80:
grade = 'B'
elif percentage >= 70:
grade = 'C'
elif percentage >= 60:
grade = 'D'
elif percentage >= 50:
grade = 'E'
else:
grade = 'Fail'
print(“The student's grade based on”, percentage, “% is”, grade)

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 :

Q6.Write a program to accept a number and display whether the


number is a palindrome or not.
Answer:
CODE :
orig=int(input("Enter the number:"))
num=orig
rev=0
while num>0:
digit=num%10
rev=rev*10+digit
num=int(num/10)
if orig==rev:
print(orig, 'is a palindrome!')
else:
print(orig, 'is not a palindrome!')

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 :

Q8.Write a program to calculate tax GST/ Income Tax.


Answer:
CODE :

original_price= float(input("Enter original price (in Rs):"))

net_price=float(input("Enter net price (in Rs):"))

GST_amount=net_price - original_price
GST_percent= (GST_amount*100) / original_price

print("GST is ", GST_percent, "%")

OUTPUT :

Q9.Write a program that reads a line and prints its frequency chart like,

● Number of uppercase letters


● Number of lowercase letters
● Number of alphabets
● Number of digits
Answer:

CODE :

line= input("Enter a line:")


lowercount = uppercount = 0
alphacount = digitcount = 0
for a in line:
if a.islower():
lowercount+=1
elif a.isupper():
uppercount+=1
elif a.isalpha():
alphacount+=1
if a.isdigit():
digitcount+=1
print("number of lowercase letters:", lowercount)
print("number of uppercase letters:", uppercount)
print("number of alphabets:", alphacount)
print("number of digits:", digitcount)

OUTPUT :
Q10. Write a program to accept the radius of a circle from the user and
display its area.
Answer:
CODE :

radius= float(input("Enter the radius of circle (in m):"))


area=3.14*(radius)**2
print(“Area of the circle with radius”, radius, “is”,area)

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 :

Q12. Write a program to perform linear search on the given list :

List = [10,51,2,18,4,31,13,5,23,64,29]
Answer:
CODE :

list1=eval(input("Enter the list:"))


length=len(list1)
element=int(input("Enter the element to be searched for:"))
for i in range(0,length):
if element==list1[i]:
print(element,"Found at index", i)
break
else:
print(element,"not found in the list")

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 :

You might also like