0% found this document useful (0 votes)
0 views

vertopal.com_C_S_practicals (2)

The document contains a collection of Python programs that perform various tasks such as finding roots of quadratic equations, generating multiplication tables, calculating areas of shapes, detecting Armstrong numbers, and more. Each program includes user input prompts and outputs results based on the calculations performed. The document serves as a comprehensive guide for basic programming exercises and algorithms in Python.

Uploaded by

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

vertopal.com_C_S_practicals (2)

The document contains a collection of Python programs that perform various tasks such as finding roots of quadratic equations, generating multiplication tables, calculating areas of shapes, detecting Armstrong numbers, and more. Each program includes user input prompts and outputs results based on the calculations performed. The document serves as a comprehensive guide for basic programming exercises and algorithms in Python.

Uploaded by

kaidemonk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

# 1.

) Program to find roots of quadratic equation


import math
print("welcome to quadratic equation root finder!")
a=int(input("enter the coefficient of x2 or a: "))
b=int(input("enter the coefficient of x or b: "))
c=int(input("enter the value of constant: "))
discriminate=math.sqrt(((b*b)-(4*a*c)))
root1=((-b+(discriminate))/(2*a))
root2=((-b-(discriminate))/(2*a))
print("the roots are: ", root1 ,"and", root2)

welcome to quadratic equation root finder!


enter the coefficient of x2 or a: 1
enter the coefficient of x or b: 6
enter the value of constant: 9
the roots are: -3.0 and -3.0

#2.) Program to print multiplication table for a number


import math
print("welcome to table generator")
num=int(input("which numbers multiplication table would you like to
generate? "))
n=int(input("enter the number up to which the table should be
generated (x10,x11,x12): "))
sum=0
while sum <= (n):
x=int(num*sum)
print(x)
sum+=1

welcome to table generator


which numbers multiplication table would you like to generate? 4
enter the number up to which the table should be generated
(x10,x11,x12): 10
0
4
8
12
16
20
24
28
32
36
40

#3.) Menu driven program to find the area of


rectangle,square,square,exit

print("welcome to shape area calculator")


print("code for rectangle is: 1")
print("code for square is: 2")
print("code for circle is: 3")
print("code for exiting the program is: 4")
n=int(input("please enter your code: "))
if n==4:
print("program termination successful")
while n < 4 and n>0:
if n == 1:
length=float(input("enter the length of rectangle in cm: "))
breadth=float(input("enter the breadth of rectangle in cm: "))
rectarea=(length*breadth,"cm2")
print(rectarea)
break

elif n == 2:
side=float(input("enter side length of square in cm: "))
squarea=(side*side,"cm2")
print(squarea)
break
else:
radi=float(input("enter the radius of circle in cm: "))
circarea=((3.14*(radi*radi)),"cm2")
print(circarea)
break

print("thank you and visit again!")

welcome to shape area calculator


code for rectangle is: 1
code for square is: 2
code for circle is: 3
code for exiting the program is: 4
please enter your code: 2
enter side length of square in cm: 2
(4.0, 'cm2')
thank you and visit again!

#4.)To check whether a number is an armstrong number


print("welcome to armstrong number detector.")
num=(input("enter the number that you want to analyze: "))
length=len(num)
sum=0
for i in (num):
i=int(i)
x=(i**length)
sum+=(x)

num=int(num)
if sum==num:
print("given number is an armstrong number")
else:
print("given number is not an armstrong number")

welcome to armstrong number detector.


enter the number that you want to analyze: 153
given number is an armstrong number

#5.)program to find the sum of digits of a number


print("sum of digits calculator")
num=input("enter the number whose sum of digits you require: ")
length=len(num)

sum=0
for i in (num):
i=int(i)
sum+=i

print("the sum of the digits of given number is: ", sum)

sum of digits calculator


enter the number whose sum of digits you require: 876
the sum of the digits of given number is: 21

#6.)wap for finding the sum of following series


#1+x2+x3+x4+……………
#x-x2/3!+x3/5!-x4/7!+……………

print("wap for first series")


exp=int(input("enter the value of the exponent upto which you want
series to continue: "))
base=int(input("enter the value of the number which you want to
exponentiate: "))

sum=1
n=2
while n<=exp:
x=(base**n)
sum+=x
n+=1

print("the sum of the series is: ", sum)

import math
print("wap for second series")
exp=int(input("enter the value of the exponent upto which you want
series to continue: "))
base=int(input("enter the value of the number which you want to
exponentiate: "))

sum=0
n=2
z=3

while exp>=n:
x=(base)
y=(base**n)
w=((base)-(base**n)/(math.factorial(z)))
sum+=w
n+=1
z+=2

print("the sum of series is: ", sum)

wap for first series


enter the value of the exponent upto which you want series to
continue: 5
enter the value of the number which you want to exponentiate: 4
the sum of the series is: 1361
wap for second series
enter the value of the exponent upto which you want series to
continue: 7
enter the value of the number which you want to exponentiate: 6
the sum of series is: 27.92021478521478

#Menu driven program to find print the following pattern:

#1 A

#1 2 B C

#1 2 3 D E F

#1 2 3 4 G H I
J

print("wap for first series:")


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

print("wap for second series:")


char="ABCDEFGHIJ"
index=0
for i in range(1,5):
for j in range(1,i+1):
print(char[index],end=" ")
index+=1
print()

wap for first series


1
1 2
1 2 3
1 2 3 4
wap for second series
A
B C
D E F
G H I J

#8.)Program to check whether a given string is palindrome or not.

string=input("enter the string that you want to analyze: ")


oppostring=string[::-1]
if string==oppostring:
print("the given string is palindrome")
else:
print("the given string is not a palindrome")

enter the string that you want to analyze: malayalam


the given string is palindrome

#9.)Program to check the longest substring in a given string sentence

string=input("enter the string sentence that you would like to


analyze: ")
s=string.split(" ")
print(s)
maxlen=0
longestsub=" "

for i in (s):
if len(i)>maxlen:
maxlen=len(i)
longestsub=i

print("the longest substring is", longestsub,"with a length


of",maxlen)
enter the string sentence that you would like to analyze: I love
python
['I', 'love', 'python']
the longest substring is python with a length of 6

#10.)wap to check whether a particular word exists in string or not

string=input("enter the string which is to be analyzed: ")


string.lower()
s=string.split(" ")
word=input("enter the word which is to be checked for: ")
word.lower()
if word in s:
print('yes the word exists in the given string.')
else:
print("the given word does not exist in the given string")

enter the string which is to be analyzed: hello world


enter the word which is to be checked for: hello
yes the word exists in the given string.

#11.)Program to count the number of uppercase letters, lower case


letters, digits in a given string.

string=input("enter the string that needs to be analyzed: ")


print(string.replace(" ",""))

countu=0
countl=0
countd=0
for i in string:
if i.isupper()==True:
countu+=1
elif i.islower()==True:
countl+=1
elif i.isdigit()==True:
countd+=1
print("count of uppercase is: ", countu)
print("count of lowercase is: ", countl)
print("count of digit is: ", countd)

enter the string that needs to be analyzed: This is earth 0132


Thisisearth0132
count of uppercase is: 1
count of lowercase is: 10
count of digit is: 4

#12.)Program to split the list L into L1 and L2. L1 having all


positive and L2 having all odd numbers.
n=int(input("enter the number of elements you want to add to main
list: "))
L=[]
for i in range (n):
ele=int(input("enter the element: "))
L.append(ele)

L1=[]
L2=[]

for i in L:
if i > (0):
L1.append(i)
if ((i)%(2)) != (0):
L2.append(i)

print("list with all positive is", L1)

print("list with all odd is", L2)

enter the number of elements you want to add to main list: 3


enter the element: 21
enter the element: 22
enter the element: 23
list with all positive is [21, 22, 23]
list with all odd is [21, 23]

#13.) WAP to find largest element in a list

n=int(input("enter the number of elements in your list: "))


L=[]
for i in range (n):
ele=int(input("enter the element: "))
L.append(ele)

largele=max(L)
print(largele,"is the largest element")

enter the number of elements in your list: 2


enter the element: 1
enter the element: 9
9 is the largest element
#14.) WAP to perform linear and binary search

print("welcome to linear and binary search engine")

print("select your engine:")


print("code for linear search: 1")
print("code for binary search: 2")
print(int(input('enter code for required engine')))

L=[]
n=int(input("enter the number of elements in your list: "))
for i in range(n):
ele=int(input("enter the element: "))
L.append(ele)
position=L[i]

print("the contents of the list are", L)

x=(int(input("enter the required element: ")))

found = False
for i in range(len(L)):
if L[i] == x:
print(f"{x} is the element required and lies at index: {i}")
found = True
break # Stop searching once the element is found

# If element is not found


if found == False:
print("Element does not exist in the list.")

welcome to linear and binary search engine


select your engine:
code for linear search: 1
code for binary search: 2
enter code for required engine1
1
enter the number of elements in your list: 3
enter the element: 4
enter the element: 3
enter the element: 7
the contents of the list are [4, 3, 7]
enter the required element: 2
Element does not exist in the list.

#15.) WAP to find sum of rows and columns separately


L=[]
r=int(input("enter the number of rows required: "))
c=int(input("enter the number of columns required: "))

for i in range(r):
L1=[]
for j in range(c):
ele=int(input("enter the element: "))
L1.append(ele)
L.append(L1)

for i in range(r):
for j in range(c):
print(L[i][j],end=" ")
print()

for i in range(r):
rs=0
for j in range(c):
rs+=L[i][j]
print("the sum of each row is: ", rs)

for i in range(c):
cs=0
for j in range(r):
cs+=L[j][i]
print("the sum of each column is: ", cs)

enter the number of rows required: 2


enter the number of columns required: 2
enter the element: 3
enter the element: 4
enter the element: 5
enter the element: 6
3 4
5 6
the sum of each row is: 7
the sum of each row is: 11
the sum of each column is: 8
the sum of each column is: 10

#16.) WAP to find transpose of a matrix

L=[]
r=int(input("enter the number of rows required: "))
c=int(input("enter the number of columns required: "))

for i in range(r):
L1=[]
for j in range(c):
ele=int(input("enter the element: "))
L1.append(ele)
L.append(L1)

for i in range(r):
for j in range(c):
print(L[i][j], end=" ")
print()
print("the above is the original matrix")

for i in range(c):
for j in range(r):
print(L[j][i], end=" ")
print()
print("the above is the required transpose!")

enter the number of rows required: 3


enter the number of columns required: 2
enter the element: 4
enter the element: 5
enter the element: 6
enter the element: 7
enter the element: 8
enter the element: 9
4 5
6 7
8 9
the above is the original matrix
4 6 8
5 7 9
the above is the required transpose!

#17.)Create a list of n students with roll no, name, stream, and


marks.

#Write a menu driven program to:

# 1. Add new students

# 2. Display the details of those students whose marks > 85

# 3. Delete a particular record based on roll no

# 4. Update the record

# 5. Display the list

L = []
n = int(input("enter the number of students you would like to record:
"))

for i in range(n):
l1 = []
rollno = int(input("enter the roll number of student (no 2
students may have the same roll number): "))
name = input("enter the name of student: ")
stream = input("enter the stream of student: ")
marks = int(input("enter the mark of student (out of 100): "))
l1 = [rollno, name, stream, marks]
L.append(l1)

print("for adding new students: press 1")


print("for displaying students with distinction: press 2")
print("for deleting a student based on roll number: press 3")
print("for updating the record: press 4")
print("for displaying the list: press 5")
code = int(input("please enter your code: "))

if code == 1:
ndash = int(input("enter the number of students you would like to
add: "))
for i in range(ndash):
l1 = []
rollno = int(input("enter the roll number of student: "))
name = input("enter the name of student: ")
stream = input("enter the stream of student: ")
marks = int(input("enter the mark of student (out of 100): "))
l1 = [rollno, name, stream, marks]
L.append(l1)
print(L)
print("list has been updated")

elif code == 2:
found = False # Flag to check if any student has distinction
for i in L:
if i[3] >= 85:
print(i[1])
found = True
if not found:
print("no students have distinction")
else:
print("the above are the students with distinction")

elif code == 3:
rollout = int(input("enter the roll number of student to be
expelled: "))
found=False
for i in L:
if i[0] == rollout:
L.remove(i)
found= True
break
if found:
print("the student has been deleted")
else:
print("no such roll number found")
print(L)

elif code == 4:
rollin = int(input("enter the rollnumber whose record needs to be
updated"))
namenew = input("enter the new name of student: ")
streamnew = input("enter the new stream: ")
marksnew = int(input("enter the marks need to be updated: "))

for i in L:
if i[0] == rollin:
i[1] = namenew
i[2] = streamnew
i[3] = marksnew

elif code == 5:
print(L)

else:
print("visit again")

enter the number of students you would like to record: 3


enter the roll number of student (no 2 students may have the same roll
number): 21
enter the name of student: Kailash
enter the stream of student: Science
enter the mark of student (out of 100): 90
enter the roll number of student (no 2 students may have the same roll
number): 10
enter the name of student: utkarsh
enter the stream of student: commerce
enter the mark of student (out of 100): 85
enter the roll number of student (no 2 students may have the same roll
number): 20
enter the name of student: varathan
enter the stream of student: humanities
enter the mark of student (out of 100): 95
for adding new students: press 1
for displaying students with distinction: press 2
for deleting a student based on roll number: press 3
for updating the record: press 4
for displaying the list: press 5
please enter your code: 5
[[21, 'Kailash', 'Science', 90], [10, 'utkarsh', 'commerce', 85], [20,
'varathan', 'humanities', 95]]

#18.) WAP to find sum of elements in a tuple

n=int(input("enter the number of elements you want in the tuple: "))


L=[]
sum=0
for i in range(n):
x=int(input("enter the element: "))
L.append(x)
sum+=x
tupleboi=tuple(L)
print("the tuple is", tupleboi)
print("sum of elements of tuple is: ", sum)

enter the number of elements you want in the tuple: 2


enter the element: 45
enter the element: 45
the tuple is (45, 45)
sum of elements of tuple is: 90

#19.) WAP to create a tuple of first nine terms of fibonacci series


n=9
a=0
b=1
fibolist=[]

for i in range(n):
fibolist.append(a)
c=a+b
a=b
b=c

print(tuple(fibolist))

(0, 1, 1, 2, 3, 5, 8, 13, 21)

#20.)WAP to create a tuple of names and display only those names whose
second character is ‘A’ or ‘a’.

n=int(input("enter the number of names required: "))


namelist=[]
for i in range(n):
name=input("enter the name: ")
namelist.append(name)
l=[]
for j in namelist:
if len(j) >1 and (j[1]==("a") or j[1]==("A")):
l.append(j)

print(tuple(l),"are the names whose second character is A or a")

enter the number of names required: 3


enter the name: Anna
enter the name: Maria
enter the name: VANDER
('Maria', 'VANDER') are the names whose second character is A or a

#21.)wap to create nested tuple and find the largest element in the
tuple

nmain = int(input("Enter the number of elements you require in the


main tuple: "))
L = []
for i in range(nmain):
ele = int(input("Enter the element: "))
L.append(ele)

tuple1 = tuple(L)

l = []
n=int(input("enter the number of elements you require in nested tuple:
"))

for i in range(n):
fac=int(input("enter the factor: "))
l.append(fac)

tuple2= tuple(l)

tuple0 = tuple1 + (tuple2,)


print(tuple0)

maxele=0

for i in tuple0:
if type(i)==tuple:
for j in i:
if j > maxele:
maxele=j
elif i> maxele:
maxele=i

print("The largest element in the tuple is: ", maxele)

Enter the number of elements you require in the main tuple: 3


Enter the element: 1
Enter the element: 4
Enter the element: 7
enter the number of elements you require in nested tuple: 2
enter the factor: 9
enter the factor: 8
(1, 4, 7, (9, 8))
The largest element in the tuple is: 9

#22.)Program to create names of employees and their salaries as input


and store them in a dictionary. Here n is to input by the user.

n=int(input("enter the number of employees required: "))


dict1={

}
for i in range(n):
emp=input("enter name of employee: ")
sal=int(input("enter salary of employee (aed/month): "))
dict1[emp]=sal

print(dict1, "is the dictionary")

enter the number of employees required: 2


enter name of employee: sara
enter salary of employee: 13000
enter name of employee: maria
enter salary of employee: 20000
{'sara': 13000, 'maria': 20000} is the dictionary

#23.)Create a dictionary called TEACHERS with subject taught as the


key and name of the teacher as values.
#Modify the teacher’s name of the subject “English” and display the
updated dictionary.

n=int(input("enter the number of teachers required: "))


dict2={

for i in range(n):
subj=input("enter the subject of teacher: ")
name=input("enter the name of teacher: ")
dict2[subj]=name
print(dict2,"is the original dictionary")

modified=input("enter the new name of english teacher: ")

dict2["English"]=modified

print(dict2,"is the updated dictionary")

enter the number of teachers required: 3


enter the subject of teacher: maths
enter the name of teacher: reema
enter the subject of teacher: english
enter the name of teacher: melissa
enter the subject of teacher: bio
enter the name of teacher: samuel
{'maths': 'reema', 'english': 'melissa', 'bio': 'samuel'} is the
original dictionary
enter the new name of english teacher: neha
{'maths': 'reema', 'english': 'melissa', 'bio': 'samuel', 'English':
'neha'} is the updated dictionary

#24.)Write a program to convert a number entered by the user into its


corresponding
#number in words. for example: if the input is 876 then the output
should be
#‘Eight Seven Six’.

dictnum={
"0":"Zero",
"1":"One",
"2":"Two",
"3":"Three",
"4":"Four",
"5":"Five",
"6":"Six",
"7":"Seven",
"8":"Eight",
"9":"Nine"
}

num=input("enter number for analysis: ")

for i in num:
if i in dictnum:
print(dictnum[i], end=" ")

enter number for analysis: 78797


Seven Eight Seven Nine Seven
# 25.)Write a program to input your friend’s, names and their phone
numbers and store them in the dictionary as the key-value pair.

# Perform the following operations on the dictionary:

# Display the Name and Phone number for all your friends:DONE

# Add a new key-value pair in this dictionary and display the modified
dictionary: DONE

# Delete a particular friend from the dictionary: DONE

# Modify the phone number of an existing friend: DONE

# Check if a friend is present in the dictionary or not: DONE

# Display the dictionary in sorted order of names: DONE

friend = {

n = int(input("enter the number of friends you would like to have: "))

for i in range(n):
name = input("enter the name of your friend: ")
number = int(input("enter the number of your friend: "))
friend[name] = number

print("the original dictionary is: ", friend)

friend["tokyo"] = (4)

print("the updated dictionary is: ", friend)

del friend["tokyo"]

print("the modified dictionary is: ", friend)

mod=input("enter the name of the existing friend whose number needs to


be changed: ")
numod=int(input("enter the modified number: "))

friend[mod]=numod

print("further modified dictionary is: ", friend)

name=input("enter the name of existing friend whose presence you want


to confirm: ")
for i in friend.keys():
if i==(name):
print("the required friend exists")
break
else:
print("the required friend does not exist")

sorteddict=dict(sorted(friend.items()))
print("the sorted dictionary is: ", sorteddict)

enter the number of friends you would like to have: 3


enter the name of your friend: denver
enter the number of your friend: 1
enter the name of your friend: tokyo
enter the number of your friend: 2
enter the name of your friend: berlin
enter the number of your friend: 3
the original dictionary is: {'denver': 1, 'tokyo': 2, 'berlin': 3}
the updated dictionary is: {'denver': 1, 'tokyo': 4, 'berlin': 3}
the modified dictionary is: {'denver': 1, 'berlin': 3}
enter the name of the existing friend whose number needs to be
changed: tokyo
enter the modified number: 4
further modified dictionary is: {'denver': 1, 'berlin': 3, 'tokyo':
4}
enter the name of existing friend whose presence you want to confirm:
denver
the required friend exists
the sorted dictionary is: {'berlin': 3, 'denver': 1, 'tokyo': 4}

You might also like