PRACTICAL LIST XII CS Solutions
PRACTICAL LIST XII CS Solutions
SOURCE CODE:
for i in range(2,num):
if num%i==0:
break;
else:
SOURCE CODE:
n=num res=0
while num>0:
rem=num%10
res=rem+res*10
num=num//10
if res==n:
print("Number is Palindrome")
else:
SOURCE CODE:
x=(1+r/100)**t
SOURCE CODE:
var=True
while var:
if choice==1:
print(ord(ch))
elif choice==2:
print(chr(val))
else:
option=input()
if option=='y' or option=='Y':
var=True
else:
var=False
5. Write a program to input a character and to print whether a given character is an alphabet, digit or any other
character.
SOURCE CODE:
if ch.isalpha():
elif ch.isdigit():
elif ch.isalnum():
else:
print(ch, "is a special symbol")
SOURCE CODE:
def factorial(n):
if n == 1:
return n
else:
return n*factorial(n-1)
if num < 0:
elif num == 0:
else:
SOURCE CODE:
def fibonacci(n):
if n<=1:
return n
else:
return(fibonacci(n-1)+fibonacci(n-2))
for i in range(num):
SOURCE CODE:
def Binary_Search(sequence, item, LB, UB):
if LB>UB:
mid=int((LB+UB)/2)
if item==sequence[mid]:
return mid
elif item<sequence[mid]:
UB=mid-1
else:
LB=mid+1
n=len(L)
found=Binary_Search(L,element,0,n-1)
if found>=0:
else:
SOURCE CODE:
def isStringPalindrome(str):
if len(str)<=1:
return True
else:
if str[0]==str[-1]:
return isStringPalindrome(str[1:-1])
else:
return False
#__main__
y=isStringPalindrome(s)
if y==True:
print("String is Palindrome")
else:
10. Write a program to count the number of vowels present in a text file.
SOURCE CODE:
fin=open("D:\\python programs\\Book.txt",'r')
str=fin.read( )
count=0
for i in str:
count=count+1
print(count)
11.
Write a program to write those lines which have the character 'p' from one text file to another text file.
SOURCE CODE:
fin=open("E:\\book.txt","r")
fout=open("E:\\story.txt","a")
s=fin.readlines()
for j in s:
if 'p' in j:
fout.write(j)
fin.close()
fout.close()
SOURCE CODE:
fin=open("D:\\python programs\\Book.txt",'r')
str=fin.read( )
L=str.split()
count_words=0
for i in L:
count_words=count_words+1
print(count_words)
13.
Write a python function sin(x,n) to calculate the value of sin(x) using its taylor series expansion up to n terms.
SOURCE CODE:
if k<=1:
return 1
else:
return k*fact(k-1)
sum=0
for i in range(step+1):
sum+=(math.pow(-1,i)*math.pow(x,2*i+1))/fact(2*i+1)
14.
Write a program to generate random numbers between 1 to 6 and check whether a user won a lottery or not.
SOURCE CODE:
if n==guess:
else:
print("Sorry, Try again, The lucky number was : ", n)
SOURCE CODE:
#Rect.py
class Rectangle:
def __init__(self):
print("Rectangle")
self.l=length
self.w=width
#Sq.py
class Square:
def __init__(self):
print("Square")
self.a=side
#Tri.py
class Triangle:
def __init__(self):
print("Trinagle")
self.b=base
self.h=height
ar= (1/2)*self.b*self.h
#main.py
OUTPUT:
Rectangle Area of Rectangle is : 200 Square Area of square is : 100 Trinagle Area of Triangle is : 24.0
16.
Write a program to plot a bar chart in python to display the result of a school for five consecutive years.
SOURCE CODE:
import matplotlib.pyplot as pl
SOURCE CODE:
import matplotlib.pyplot as pl
import numpy as np
x= np.linspace(-50,50)
y= x**2
pl.plot(x,y,linestyle='-')
pl.show( )
18.
Write a program in python to plot a pie chart on consumption of water in daily life.
SOURCE CODE:
import matplotlib.pyplot as pl
consumption=[5,30,50,3]
pl.show( )
SOURCE CODE:
n=len(L)
for i in range(n):
if L[i]==item:
break
else:
SOURCE CODE:
n=len(L)
for p in range(0,n-1):
for i in range(0,n-1):
if L[i]>L[i+1]:
t=L[i]
L[i]=L[i+1]
L[i+1]=t
21. Write a menu based program to perform the operation on stack in python.
22. Write a menu based program to perform the operation on queue in python.