11 CS Practical File

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Practical file

Q.1 Write a program to take input as a string from the user and reverse each occurrences of the
string ?
Sol:
s=input('enter string')
i=0
length=len(s)-1
#temp=' '
#rev=' '
temp,rev='',''
while i<=length:
if s[i]!=' ':
temp=s[i]+temp
else:
rev=rev+temp+" "
temp=''

i=i+1
rev=rev+temp
print("the final reverse is:",rev)

Output
enter stringNacre software solution
the final reverse is: ercaN erawtfos noitulos
Q.2 Write a program to take number as a input from the user and print table of a given number ?
Sol:
N=int(input("Enter a number"))
for i in range(1,11):
print(N,"*",i,"= is",N*i)

O/P
Enter a number5
5 * 1 = is 5
5 * 2 = is 10
5 * 3 = is 15
5 * 4 = is 20
5 * 5 = is 25
5 * 6 = is 30
5 * 7 = is 35
5 * 8 = is 40
5 * 9 = is 45
5 * 10 = is 50
Q.3 WAP to take input as a number from the user and print reverse of the given
number ?
Answer:
N=int(input("Enter a number:"))
reverse,r=0,0
while N>0:
r=N%10
reverse=reverse*10+r
N//=10
print("the reverse of a number is:",reverse)
O/P
Enter a number:5678
the reverse of a number is: 8765

Q.4 Write a program to take input as a number from the user and check
whether it is a perfect number or not ?
N=int(input("Enter a number:"))
sum=0
for i in range(1,N//2+1):
if N%i==0:
sum=sum+i
if N==sum:
print(N,":is perfect")
else:
print(N,":is not perfect")

O/P
Enter a number:28
28 :is perfect
Enter a number:6
6 :is perfect
Q.5 Write a program to print following pattern ?
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
Sol:
N=int(input("Enter your Number:"))
i=1
for r in range(1,N+1):
for c in range(1,r+1):
print(r*c,end=" ")
print()

Q.6 Write a program to accept list as input and add 5 in all the odd values
and 10 in all the even values of the list L.
Sol:
L=[1,7,6,5,4,10]
for i in range(0,len(L)):
if L[i]%2==0:
L[i]=L[i]+10
else:
L[i]=L[i]+5
print(“fresh list after modifying:”,L)
O/P
Fresh list after modifying: [6,12,16,10,14,20]

Q.7 Write a program to accept list as a input from the user and perform the
linear search.
Sol:
L=[]
n=int(input("how many size of list !you wish to enter"))
print("Enter list elements")
for i in range(n):
L.append(i+1)
Flag=False
print("entered items are:",L)
search=int(input("enter item you want to search"))
for i in range(len(L)):
if L[i]==search:
print("element found at",i,"index")
Flag=True
Break
if Flag==False:
print("element not found")

O/P
how many size of list !you wish to enter5
Enter list elements
entered items are: [1, 2, 3, 4, 5]
enter item you want to search4
element found at 3 index

Q.8 WAP to find the largest number among the 3 inputted numbers ?
Sol:

A=int(input("Enter first number:"))


B=int(input("Enter second number:"))
C=int(input("Enter Third number:"))
if (A>B):
if(A>C):
print(A,"is the largest number")
else:
print(C,"is the largest number")
else:
if (B>C):
print(B,"is the largest number")
else:
print(C,"is the largest number")
O/P
Enter first number:10
Enter second number:20
Enter Third number:30
30 is the largest number
Enter first number:5
Enter second number:90
Enter Third number:30
90 is the largest number
Q.9 Write a program in Python to find the minimum values of a tuples ?
Sol:
t=(9,8,5,251,2)
min=t[0]
for i in range(len(t)):
if t[i]<min:
min=t[i]
print("minimum values among list is:",min)
O/P
minimum values among list is: 2

Q.10 Write a program to accept values from a user. Add a tuple to it and display its
elements one by one.
Sol:
t=tuple()
n=int(input("Enter any number:"))
i=1
while i<=n:
a=int(input("Enter number"))
t=t+(a,)
i=i+1
print("The output is:")
for i in t:
print(i,end=” “)
O/P
Enter any number:4
Enter number1
Enter number10
Enter number12
Enter number13
The output is:
1 10 12 13
Q.11 WAP to take string as a input from the user and display count of occurrences of
each the word in the string ?
Sol:
Word=input("Enter some word:")
d={}
for x in Word:
d[x]=d.get(x,0)+1
print(d)
for k,v in d.items():
print(k,"occured....",v,"times")

O/P
Enter some word:Python Programming
{'P': 2, 'y': 1, 't': 1, 'h': 1, 'o': 2, 'n': 2, ' ': 1, 'r': 2, 'g': 2, 'a': 1, 'm': 2, 'i': 1}
P occured.... 2 times
y occured.... 1 times
t occured.... 1 times
h occured.... 1 times
o occured.... 2 times
n occured.... 2 times
occured.... 1 times
r occured.... 2 times
g occured.... 2 times
a occured.... 1 times
m occured.... 2 times
i occured.... 1 times

Q.12 Write a program to take Number as a input from the user and print corresponding day of the week ?
Solution:
Day=int(input("Enter day number:"))
if Day==1:
print("Monday")
elif Day==2:
print("Tuesday")
elif Day==3:
print("Wednesday")
elif Day==4:
print("Thursday")
elif Day==5:
print("Friday")
elif Day==6:
print("Saturday")
elif Day==7:
print("Sunday")
else:
print("invalid day")

O/P
Enter day number:1
Monday
Enter day number:2
Tuesday
Enter day number:8
invalid day

Q.13 Write a program to accept decimal number as a input and display its binary number ?
Answer:
n=int(input("Enter any number:"))
a=0
m=0
c=1
while n>0:
a=n%2
m=m+(a*c)
c=c*10
n=int(n/2)
print("corresponding binary number is:",m)

O/P
Enter any number:20
corresponding binary number is: 10100

Q.14 Program to create a dictionary with name of the employees and their salary
dynamically ?
Sol:
num=int(input("enter the number of employees whose data to be stored"))
count=1
employee={}
while count<=num:
name=input("Enter the name of the employee:")
salary=int(input("Enter the salary:"))
employee[name]=salary
count+=1
print(employee)
print("\n\nEMPLOYEE \t SALARY")
for k in employee:
print(k,"\t\t",employee[k])

O/P
enter the number of employees whose data to be stored3
Enter the name of the employee:Raja
Enter the salary:6000
Enter the name of the employee:Umesh
Enter the salary:5000
Enter the name of the employee:Virat
Enter the salary:7000
{'Raja': 6000, 'Umesh': 5000, 'Virat': 7000}

EMPLOYEE SALARY
Raja 6000
Umesh 5000
Virat 7000

Q.15 Write a program to check whether a given number is even or odd ?


Answer:
N=int(input("Enter a Number:"))
if N%2==0:
print(N,"is even")
else:
print(N,"is odd")

O/P
Enter a Number:15
15 is odd

16. Write a program to print prime number from 1 to 100 ?


Answer:
minimum = int(input(" Please Enter the Minimum Value: "))
maximum = int(input(" Please Enter the Maximum Value: "))
for Number in range (minimum, maximum + 1):
count = 0
for i in range(2, (Number//2 + 1)):
if(Number % i == 0):
count = count + 1
break
if (count == 0 and Number != 1):
print(" %d" %Number, end = ' ')

O/P
Please Enter the Minimum Value: 1
Please Enter the Maximum Value: 100
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83
89 97

17. WAP to exchange first-half elements of list with second-half elements assuming list is
having even number of elements.
Sol:
L=[10,20,30,40,50,60,70]
x=int(len(L)/2)
for i in range(x):
L[i],L[i+1]=L[x+i],L[i]
print("list after arranging:",L)

18. Write a program to input ‘n’ employees’ salary and find minimum & maximum
salary among ‘n’ employees
Sol:
t=tuple( )
n=input(“Enter total number of employees”)
for i in range(n):
a=input(“enter salary of employee:”)
t=t+(a,)
print( “maximum salary=”,max(t))
print( “minimum salary=”,min(t))

Output :
Enter total number of employees 3
enter salary of employee: 7800
enter salary of employee: 8934
enter salary of employee: 6544
maximum salary = 8934
minimum salary = 6544

19. Write a program to take tuple as a input from the user and find mean of a tuple.
Sol:
t=tuple()
n=int(input("Enter any number:"))
i=1
while i<=n:
a=int(input("Enter number"))
t=t+(a,)
i=i+1
print("tuples elements are:",t)
length=len(t)
sum=0
for i in range(0,length):
sum=sum+t[i]
print("\n mean of a tuples are:")
mean=sum/length
print(mean)

O/p
Enter any number:5
Enter number1
Enter number2
Enter number45
Enter number67
Enter number34
tuples elements are: (1, 2, 45, 67, 34)
mean of a tuples are:
29.8

20. WAP to display frequencies of all the elments of a list.


L=[3,21,5,6,3,8,21,6,6,21]
unique=[]
duplicate=[]
for i in L:
if i not in unique:
unique.append(i)
elif i not in duplicate:
duplicate.append(i)
else:
pass
print("unique elements are:",unique)
print("duplicate elements are:",duplicate)

Output
unique elements are: [3, 21, 5, 6, 8]
duplicate elements are: [3, 21, 6]

You might also like