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

Computer Science Prog File

The document outlines a Computer Science program for the academic year 2024-2025, detailing various programming tasks and functions related to number checking, file handling, and database connectivity. It includes specific programs for checking prime numbers, perfect numbers, Armstrong numbers, and implementing file operations such as reading, writing, and deleting records in binary and CSV formats. Additionally, it covers SQL queries and connecting Python with MySQL for database operations.

Uploaded by

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

Computer Science Prog File

The document outlines a Computer Science program for the academic year 2024-2025, detailing various programming tasks and functions related to number checking, file handling, and database connectivity. It includes specific programs for checking prime numbers, perfect numbers, Armstrong numbers, and implementing file operations such as reading, writing, and deleting records in binary and CSV formats. Additionally, it covers SQL queries and connecting Python with MySQL for database operations.

Uploaded by

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

COMPUTER

SCIENCE
PROGRAM
FILE
Academic Year : 2024-2025

Board Roll No –
Name – Anvay Asri
School Roll No – 06
Class – XII – C
Subject – Computer Science
Subject code- 083

Index
S.
Topic
No
To check whether the given number is
1
prime or not
To check whether the given number is
2
perfect or not
To check whether the given number is
3
an Armstrong number or not
To check whether the given number is
4
a Palindrome or not
Write a function for the factorial of a
5
number
Write a function for the Fibonacci
6
series
Write a function to read a text file line
7 by line and display each word
separated by a #
8 Read a text file and display the
number of vowels, consonants,
uppercase, and lowercase characters
in the file
Write a program to count the number
9
of words in a file
Write a program to remove all lines
10 that contain the character 'a' in a file
and write it to another file
Create a binary file with roll number,
11 name, and marks. Input a roll number
and update the marks
Write a program to read and write
12
employee records in a binary file
Write a program to enter the following
records in a binary file: Item No
(integer), Item Name (string), Qty
(integer), Price (float). Number of
records to be entered should be
13
accepted from the user. Read the file
to display the records in the following
format: Item No, Item Name, Quantity,
Price per item, Amount (calculated as
Price * Qty)
Program to delete a specified record
14
from a binary file
Create a CSV file by entering user ID
15 and password. Search the password for
the given user
16 To perform read and write operations
in a CSV file
Write a program to generate random
numbers between 1 to 6 and check
17
whether a user has won the lottery or
not
To implement a stack using the list
18
data structure
19 SQL Queries
20 Aggregate functions in SQL
Connect to Python with MySQL using
21
database connectivity

PROGRAM - 1:
AIM: To check whether the given number is prime or not.

INPUT:
x=int(input("Enter Number"))
count=0
for i in range(2,(x//2+1)):
if (x%i==0):
count=count+1
break
if (count==0 and x!=1):
print(x,"is a prime number")
elif(x==1):
print("neither prime nor composite")
else:
print(x,"is not a prime number")

OUTPUT:

PROGRAM 2 :
AIM: To check whether the given number is perfect or
not.
INPUT :
x=int(input("Enter Number"))
endresult=0
for i in range(1,x):
if(x%i==0):
endresult=endresult+i
break
if(endresult==x):
print("Yes , it is a perfect number")
else:
print("No, it is not a perfect number")

OUTPUT:

PROGRAM 3 :
AIM: To check whether the given number is an Armstrong
or not.
INPUT:
x=int(input("Enter Number: "))
sum=0
tem=x
while tem>0:
dig=tem%10
sum+=dig**3
tem//=10
if x==sum:
print(x,"is an armstrong number")
else:
print(x,"is not an armstrong number")

OUTPUT:

PROGRAM 4:
AIM: To Check whether the given number is in palindrome or not .
INPUT:
txt=input("Enter Text")
if(txt==txt[::-1]):
print("This is a Palindrome String")
else:
print("It is Not a Palindrome string")

OUTPUT:

PROGRAM 5:
AIM: Write a function for the factorial of a number .
INPUT:
x=int(input("Enter Number"))
fct=1
if x>0:
for i in range(1,x+1):
fct=fct*i
print("the Factorial of ",x,"is:",fct)
else:
print("No Factorial")

OUTPUT:

PROGRAM 6:
AIM: Write a function for the Fibonacci series
INPUT:
x=int(input("Enter the Value:"))
a=0
b=1
c=0
print("Fibbonaci series:",end=" ")
for i in range(1,x+1):
print(c,end=" ")
a=b
b=c
c=a+b

OUTPUT:

PROGRAM 7:
AIM: Write a function to read a text file line by line and
display each word separated by a #
INPUT:
def read():
f=open("text.txt","r")
a=f.read()
c=a.split()
for i in a:
print(i+"#",end="")

print()
f.close

a=read()

OUTPUT:

PROGRAM 8:
AIM: Read a text file and display the number of vowels /
consonants/ uppercase / lowercase characters in a file
INPUT:
import pickle
f=open("text.txt","r")
a=f.read()
f.close()
C_V,C_C,C_U,C_S=0,0,0,0
for ch in a:
if ch in "aeiouAEIOU":
C_V+=1
if ch not in "aieouAEIOU":
C_C+=1
if ch.isupper():
C_U+=1
if ch.islower():
C_S+=1
print("The number of vowels in the file",C_V)
print("The number of consonants in the file",C_C)
print("The number of uppercase characters in the file",C_U)

OUTPUT:
PROGRAM 9:
AIM: WAP to count number of word in a file

INPUT:
import pickle
f=open("text.txt","r")
count=0
for l in f:
words=l.split(" ")
count+=len(words)
f.close
print("no of words in a text file",count)

OUTPUT:

PROGRAM 10:
AIM: WAP to remove all the lines that contain the
character “a” in a file and write it to another file

INPUT:
f1=open("text.txt","r")
f2=open("untitled.txt","w+")
s=f1.readlines()
for j in s:
if "a"in j:
f2.write(j)
print("##file copied successfully##")
f1.close
f2.close

OUTPUT:

PROGRAM 11:
AIM: Create a binary file with roll no , name and marks ,
Input a roll no and updates the marks

INPUT:
import pickle
s={}
f=open("stud.dat","wb")
c="y"
while c=="y" or c=="Y":
rno=int(input("Enter the roll no. of the student:"))
name=input("Enter the name of the students")
marks=int(input("Enter the marks of the student:"))
s["roll no"]=rno
s["Name"]=name
s["marks"]=marks
pickle.dump(s,f)
c=input("Do you want to add more students(y/n):")
f.close()
f.open("stud.dat","rb+")
rno=int(input("enter the roll no. of the student to be updated"))
marks=int(input("enter the updated marks of the student:"))
f.seek(0,0)
m=0
try:
while True:
pos=f.tell()
s=pickle.load(f)
if s["Roll no"]==rno:
f.seek(pos)
elif s["marks"]==marks:
pickle.dump(s,f)
m=m+1
except EOFError:
f.close()

OUTPUT:

PROGRAM 12:
AIM: Write a program to read and write employee records
in binary file

INPUT:
import pickle
f=open("empfile.dat","ab")
print("Enter Records of Employees")
while True:
eno=int(input("Employee Number:"))
ename=input("Employee Name:")
ebasic=int(input("Basic Salary:"))
allow=int(input("Allowances:"))
totsal=ebasic+allow
print("TOTAL SALARY:",totsal)
edata=[eno,ename,ebasic,allow,totsal]
pickle.dump(edata,f)
ans=input("Do you wish to enter more records(y/n)?")
if ans.lower()=="n":
print("Record Entry OVER")
print()
break
print("Size of binary file(in bytes):",f.tell())
f.close()
print("Now reading the employee records from the file")
print()
readrec=1
try:
with open("empfile.dat","rb")as f:
while True:
edata=pickle.load(f)
print("record number:",readrec)
print(edata)
readrec=readrec+1
except EOFError:
f.close()

OUTPUT:
PROGRAM 13:
AIM: Write a program to enter the following records in a
binary file: Item No integer Item Name string Qty integer
Price float Number of records to be entered should be
accepted from the user. Read the file to display the
records in the following format: Item No: Item Name:
Quantity: Price per item: Amount: (to be calculated as
Price " Qty).

INPUT:
import pickle
f=open("items.dat","wb")
record={}
wish="y"
count=1
while wish.upper()=="Y":
item_no=int(input("Enter Item Number:"))
item_name=input("Enter Item Name:")
quantity=int(input("Enter Quantity:"))
price=float(input("Enter price per item"))
record["itemno"]=item_no
record["itemname"]=item_name
record["quantity"]=quantity
record["price"]=price
pickle.dump(record,f)
count=count+1
wish=input("Want to add more records(y/n):")

f.close()

import pickle
f=open("items.dat","rb")
record={}
count=1
try:
while True:
print("items",count,"details")
record=pickle.load(f)
print("item number:",record["itemno"])
print("item name:",record["itemname"])
print("quantity:",record["quantity"])
print("price:",record["price"])
count=count+1
except EOFError:
f.close()

OUTPUT:
PROGRAM 14:
AIM: Write a program to delete a specified record from
binary files

INPUT:
import pickle
fin=open("bachas.dat","wb")
for i in range(2):
rno=int(input("Enter roll no of the student:"))
name=input("Enter Student Name:")
marks=int(input("Enter Marks of the student:"))
rec=[rno,name,marks]
pickle.dump(rec,fin)
fin.close()
stu={}
def led():
file=open("bachas.dat","rb")
f=open("bachas.dat","wb")
r=int(input("Enter Roll no to be deleted"))
found=False
try:
while True:
stu=pickle.load(file)
if stu[0]==r:
found=True
else:
pickle.dump(stu,f)
except EOFError:
file.close()
f.close()
if found==False:
print("Record not found")
file1=open("bachas.dat","rb")
try:
while True:
Info= pickle.load(file1)
for i in info:
print(i)
break
except EOFError:
file1.close()
led(

OUTPUT:
PROGRAM 15:
AIM: Create a CSV file using Userid and password .
Search the password for the given user

INPUT:
import csv
with open("pass","w") as obj:
fileobj=csv.writer(obj)
fileobj.writerow(["USER ID","PASSWORD"])
while(True):
user_id=input("ENTER ID:")
password=input("Enter Password:")
record=[user_id,password]
fileobj.writerow(record)
x=input("Press Y/y to continue and N/n to terminate the
program/n")
if x in"Nn":
break
elif x in "Yy":
continue
with open("pass","r") as obj2:
fileobj2=csv.reader(obj2)
given=input("Enter the user id to be searched/n")
for i in fileobj2:
next(fileobj2)
if i[0]==given:
print(i[1])
break

OUTPUT:
PROGRAM 16:
AIM: To perform read and write operations in CSV file.

INPUT:
import csv
f=open("readwrite","w")
fieldnames=["emp_name","dept","birth_month"]
writer=csv.DictWriter(f,fieldnames=fieldnames)
writer.writeheader()
writer.writerow({"emp_name":"parker","dept":"Accounting","birt
h_month":"November"})
writer.writerow({"emp_name":"Smith","dept":"IT","birth_month":
"October"})
f.close()
f1=open("readwrite","r")
read=f1.read()
print(read)

OUTPUT:
PROGRAM 17:
AIM: WAP to select a random number between 1 and 6
and check whether the user has won a lottery or not

INPUT:
import random
def check_lottery(selected_number):
lottery_number=random.randint(1,6)
if selected_number==lottery_number:
print(lottery_number,"this is the number of lottery")
return("Congratulations!You won the Lottery")
else:
print(lottery_number,"this is the number of lottery")
return("Sorry,You Lost.")
selected_number=int(input("Enter Your Selected
Number(between 1 and 6):"))
print(check_lottery(selected_number))

OUTPUT:
PROGRAM 18:
AIM: To implement a stack using list data structures

INPUT:
def isEmpty(stk):
if stk==[]:
return True
else:
return False
def Push(stk,elt):
stk.append(elt)
print("element inserted")
print(stk)

def pop(stk):
if isEmpty(stk):
print("Stack is empty .... underflow case")
else:
print("Deleted element is:",stk.pop())
def peek(stk):
if isEmpty(stk):
print("stack is empty")
else:
print("element at top of the stack:",stk[-1])
def Display(stk):
if isEmpty(stk):
print("stack is empty")
else:
for i in range(len(stk)-1,-1,-1):
print(stk[i])
Stack=[]
while True:
print("1.Push")
print("2.Pop")
print("3.Peek")
print("4.Display")
print("5.Exit")
ch=int(input("Enter your choice:"))
if ch==1:
element=int(input("Enter the element which you want to
add"))
Push(Stack,element)
if ch==2:
pop(Stack)
if ch==3:
peek(Stack)
if ch==4:
Display(Stack)
else:
break

OUTPUT:
PROGRAM 19:
AIM: SQL Queries
PROGRAM 20:
AIM: Aggregate Function in SQL:
PROGRAM 21:
AIM: Connect to Python with MySQL using database
connectivity

INPUT:
import mysql.connector as c
db=c.connect(host="localhost",user="root",password="1
234",database="paisa")
if db_is.connected():
print("Connected to the SQL database successfully!")
else:
print("NOT CONNECTED")

OUTPUT:

You might also like