CS Practical File
CS Practical File
if option =="+":
result= value1+value2
result= value1-value2
result= value1*value2
if value2==0:
else:
result= value1/value2
result= value1//value2
else:
result= value1%value2
divsum=0
for i in range(1,num):
if num%i==0:
divsum+=i
if divsum==num:
print("Perfect Number")
else:
perfect(n)
Program 3: Write a Program to find whether the number is
an Armstrong number or not.
S ource code :
n=int(input("Enter the number:"))
n1=n
sum=0
while(n>0):
answer=n%10;
sum=sum+(answer*answer*answer)
n=int(n/10)
if sum== n1:
print("Armstrong number")
else:
newlist=[]
newlist[:0]=st
l=len(newlist)
ed=l-1
for i in range(0,l):
if newlist[i]!=newlist[ed]:
break
if i>=ed:
break
l=l-1
ed=ed-1
Program 5: Write a Program to show the Floyd's Triangle
in decreasing order.
S ource code :
n=int(input("Enter the number:"))
for i in range(n,0,-1):
for j in range(n,i-1,-1):
print(j,end=" ")
print()
Program 6: Write a Program to find the factorial using
user-defined module.
S ource code :
import factfunc
value=factfunc.fact(n)
# Module factfunc
def fact(n):
f=1;
while n>0:
f=f*n
n=n-1
return f
Program 7: Write a Program to enter the number of terms
and to print the Fibonacci Series.
S ource code :
i=int(input("Enter the limit:"))
x=0
y=1
z=1
print("Fibonacci series\n")
print(x,y,end=" ")
while(z<=i):
print(z,end=" ")
x=y
y=z
z=x+y
Program 9: Write a program to print a square
multiplication table.
S ource code:
for row in range(1,10):
product=row*col
if product<10:
print('',product,' ',end='')
else:
print(product,' ',end='')
print()
Program 10: Write a program to show the outputs based
on entered list.
S ource code:
my_list=['p','r','o','b','e']
# Output: p
print(my_list[0])
# Output: o
print(my_list[2])
# Output:e
print(my_list[4])
# my_list[4.0]
# Nested List
n_list=['Happy',[2,0,1,5]]
# Nested indexing
# Output:a
print(n_list[0][1],n_list[0][2],n_list[0][3])
#output:5
print(n_list[1][3])
Program 11: Write a program to implement all basic
operations of a stack using lists.
S ource code:
def push(L):
x=int(input("Enter a number:"))
L.append(x)
def pop(L):
if len(L)==0:
print("Stack is empty")
else:
print(L.pop(),"Deleted")
def peek(L):
top=len(L)-1
def display(L):
for i in range(len(L)-1,-1,-1):
print(L[i])
def menu():
L=[]
while True:
menu()
if ch==1:
push(L)
elif ch==2:
pop(L)
elif ch==3:
peek(L)
elif ch==4:
display(L)
elif ch==5:
break
Program 12: Write a program to implement list as a queue-
using function append() and pop().
S ource code:
a=[]
c='y'
while(c=='y'):
print("1.INSERT")
print("2.DELETE")
print("3.Display")
if (choice==1):
a.append(b)
elif (choice==2):
if(a==[]):
print("Queue Empty")
else:
a.pop(0)
elif(choice==3):
l=len(a)
for i in range(0,l):
print(a[i])
else:
print("Wrong Input")
print(f.name)
f_contents=f.read()
print(f_contents)
f_contents=f.readlines()
print(f_contents)
f_contents=f.readline()
print(f_contents)
for line in f:
print(line,end='')
f_contents=f.read(50)
print(f_contents)
size_to_read=10
f_contents=f.read(size_to_read)
while len(f_contents)>0:
print(f_contents)
print(f.tell())
f_contents=f.read(size_to_read)
Program 14: Write a program to read data from data file in
read mode and count the particular word occurrence in
given string.
S ource code:
f=open("poem.txt",'r')
read=f.readlines()
f.close
times=0
times2=0
count=0
times+=1
line2=each
times2+=1
if chk==line2:
count+=1
print(times)
print(times2)
Program 15: Write a program to display the number of
lines in the file.
S ource code:
myfile=open("poem.txt","r")
s=myfile.readlines()
linecount=len(s)
myfile.close()