ARMY PUBLIC SCHOOL, HISAR
PROGRAM FILE
Submitted to: Submitted by:
Mr. Davinder Singh Name : Harsh Yadav
PGT Computer Science Class : XII (Non-Medical)
Roll No. : 17645652
Subject : Computer Science (083)
Session : 2021-22
\
Program 1: Program to enter two numbers and
perform the arithmetic operations like +,-,*,/,//,%.
result = 0
val1 = float(input("Enter the first value :"))
val2 = float(input("Enter the second value :"))
op = input("Enter any one of the operator (+,-,*,/,//,%)")
if op == "+":
result = val1 + val2
elif op == "-":
result = val1 - val2
elif op == "*":
result = val1 * val2
elif op == "/":
if val2 == 0:
print("Please enter a value other than 0")
else:
result = val1 / val2
elif op == "//":
result = val1 // val2
else:
result = val1 % val2
print("The result is :",result)
Program 2: Write a program to find whether an
inputted number is perfect or not.
def pernum(num):
divsum=0
for i in range(1,num):
if num%i == 0:
divsum+=i
if divsum==num:
print('Perfect Number')
else:
print('Not a perfect number')
Program 3: Write a Program to check if the
entered number is Armstrong or not.
no=int(input("Enter any number to check : "))
no1 = no
sum = 0
while(no>0):
ans = no % 10;
sum = sum + (ans * ans * ans)
no = int (no / 10)
if sum == no1:
print("Armstrong Number")
else:
print("Not an Armstrong Number")
Program 4: Write a Program to find factorial of the
entered number.
num = int(input("Enter the number for calculating its factorial : "))
fact = 1
i=1
while i<=num:
fact = fact*i
i=i+1
print("The factorial of ",num,"=",fact)
Program 5: Write a Program to enter the number
of terms and to print the Fibonacci Series.
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 6: Write a Program to enter the string
and to check if it’s palindrome or not using loop.
msg=input("Enter any string : ")
newlist=[]
newlist[:0]=msg
l=len(newlist)
ed=l-1
for i in range(0,l):
if newlist[i]!=newlist[ed]:
print ("Given String is not a palindrome")
break
if i>=ed:
print ("Given String is a palindrome")
break
l=l-1
ed = ed - 1
Program 7: Write a Program to enter the number
and print the Floyd’s Triangle in decreasing order.
n=int(input("Enter the number :"))
for i in range(5,0,-1):
for j in range(5,i-1,-1):
print (j,end=' ')
print('\n')
Program 8: Write a program to print a pattern.
for i in range(1,6):
for j in range(1,i+1):
print(j,end='')
print()
Program 9: Write a random number generator that
generates random numbers between 1 and 6.
import random
print(random.randint(1,6))
Program 10: Write a program to read a file line by
line separated by a #.
file = open(“myfile.txt”,”r”)
while True:
Print(file.readline(),sep=’#’)
Program 11: Read a text file and display the number
of vowels/consonants/uppercase/lowercase characters
in the file.
f=open("D:\\test2.txt","r")
cont=f.read()
print(cnt)
v=0
cons=0
l_c_l=0
u_c_l=0
for ch in cont:
if (ch.islower()):
l_c_l+=1
elif(ch.isupper()):
u_c_l+=1
ch=ch.lower()
if( ch in ['a','e','i','o','u']):
v+=1
elif (ch in ['b','c','d','f','g',
'h','j','k','l','m',
'n','p','q','r','s',
't','v','w','x','y','z']):
cons+=1
f.close()
print("Vowels are : ",v)
print("consonants are : ",cons)
print("Lower case letters are : ",l_c_l)
print("Upper case letters are : ",u_c_l)
Program 12: Remove all the lines that contain the
character 'a' in a file and write it to another file.
fo=open('hp.txt','r')
fi=open('writehp.txt','w')
l=fo.readlines()
for i in l:
if 'a' in i:
i=i.replace('a','')
fi.write(i)
fi.close()
fo.close()
Program 13: Create a binary file with name and
roll number. Search for a given roll number and
display the name, if not found display appropriate
message.
import pickle
#Creating the file and writing the data
f=open("records.dat", "wb")
pickle.dump(["Wakil", 1], f)
pickle.dump(["Tanish", 2], f)
pickle.dump(["Priyashi", 3], f)
pickle.dump(["Kanupriya", 4], f)
pickle.dump(["Aaheli", 5], f)
f.close()
#Opening the file to read contents
f=open("records.dat", "rb")
n=int(input("Enter the Roll Number: "))
flag = False
while True:
try:
x=pickle.load(f)
if x[1]==n:
print("Name: ", x[0])
flag = True
except EOFError:
break
if flag==False:
print("This Roll Number does not exist")
Program 14: Create a binary file with roll number,
name and marks. Input a roll number and update
the marks.
import pickle
#Creating the file and writing the data
f=open("records.dat", "wb")
#list index 0 = roll number
#list index 1 = name
#list index 2 = marks
pickle.dump([1, "A", 90], f)
pickle.dump([2, "B", 80], f)
pickle.dump([3, "C", 90], f)
pickle.dump([4, "D", 80], f)
pickle.dump([5, "E", 85], f)
f.close()
#Opeining the file to read contents
f=open("records.dat", "rb")
roll=int(input("Enter the Roll Number: "))
marks=float(input("Enter the updated marks: "))
List = [ ] #empty list
flag = False #turns to True if record is found
while True:
try:
record=pickle.load(f)
List.append(record)
except EOFError:
break
f.close()
#Reopening the file to update the marks
f=open("records.dat", "wb")
for rec in List:
if rec[0]==roll:
rec[2] = marks
pickle.dump(rec, f)
print("Record updated successfully")
flag = True
else:
pickle.dump(rec,f)
f.close()
if flag==False:
print("This roll number does not exist")
Program 15: Create a CSV file by entering user-id
and password, read and search the password for
given user id.
import csv
#User-id and password list
List = [["user1", "password1"],
["user2", "password2"],
["user3", "password3"],
["user4", "password4"],
["user5", "password5"]]
#Opening the file to write the records
f1=open("UserInformation.csv", "w", newline="\n")
writer=csv.writer(f1)
writer.writerows(List)
f1.close()
#Opening the file to read the records
f2=open("UserInformation.csv", "r")
rows=csv.reader(f2)
userId = input("Enter the user-id: ")
flag = True
for record in rows:
if record[0]==userId:
print("The password is: ", record[1])
flag = False
break
if flag:
print("User-id not found")