Professional Documents
Culture Documents
CS Assignment (AKBAR)
CS Assignment (AKBAR)
Ans 1) Code:
summ=0
for a in range(1,10):
summ += a
print('The sum of ',a,'consecutive natural numbers is',summ)
Output:
Ans 2
Code:
import math
a= eval(input('Enter the coefficient of x^2:'))
b= eval(input('Enter the coefficient of x:'))
c= eval(input('Enter the numerical term:'))
D= math.sqrt(math.pow(b,2)-(4*a*c))
if D > 0:
print('roots of equation are :',(-b+D)/2*a,'and',(-b-D)/2*a)
elif D == 0:
print('roots of equation is :',(-b+D)/2*a)
else:
print('no real root exists')
Output:
Ans 3
Code:
Output:
enter a non-negative integer: 5
The factorial of 5 is 120
Code:
def interest(p,r=10,t=2):
int_=p*r*t
print(int_)
Output:
interest(12,45,2)
1080
interest(2)
40
Ans 5)
Code:
def operations(a,b):
summ=a+b
prod=a*b
sub=a-b
div=a/b
rem=a%b
print(summ ,prod ,sub ,div ,rem)
Output:
operations(36,4)
40 144 32 9.0 0
print(15+random.random()*5)
Ans 6
import pickle
count=0
rec={}
Studat=open(‘stu.dat’,’rb’)
Try:
While true:
rec=pickle.load(Studat)
if rec[‘Marks’] > 18:
print(rec)
count += 1
if count == 0:
print(“No records found”)
Except EOFerror :
Studat.close()
Output :
Q 8) Write a program to open file Stu.dat and search for records with
roll numbers as 12 or 14. If found, display the records.
Ans 8
Code:
import pickle
rec={}
count=0
Studat= open(‘stu.dat’,’rb’)
Try:
While True:
rec=pickle.load(Studat)
if rec[‘Rollno’] in [12,14]:
print(rec)
count += 1
if count == 0 :
print(‘No records found’)
Output:
Ans 9
Code:
import csv
dataobj=open(‘result.csv’,’w’)
datawrt=csv.writer(dataobj)
winners=[
['Name','Points','Rank],
['Shradha',4500,23],
['Nischay',4800,31],
['Ali',4500,25],
['Adi',5100,14] ]
datawrt.writerows(winners)
dataobj.close()
Q 10) You created the file compresult.csv in the previous program as
shown below. Write a program to read the records of this csv file and
display them.
Ans 10
Code:
import csv
with open(‘winners.csv’,’r’,newline=’\r\n’) as wn:
wnreader = csv.reader(wn)
for a in wnreader:
print(a)
Output:
['Name','Points','Rank],
['Shradha',4500,23],
['Nischay',4800,31],
['Ali',4500,25],
['Adi',5100,14]
(a) To show Book name, Author name and Price of books of First
Publ. publishers.
(b) To list the names from books of Text type.
(c) To display the names and price from books in ascending order of
their price.
(d) To increase the price of all books of EPB Publishers by 50.
(e) To display the Book_Id, Book_Name and Quantity_Issued for all
books which have been issued.
(The query will require contents from both the tables.)
Ans 12
Code:
Q 13) Consider the following DEPT and WORKER tables. Write SQL
queries for (i) to (iv).
Ans 13
Code:
Ans 14
Code :
Q 15) Write a Python program that displays first three rows fetched
from student table of MySQL database "test”
Ans 15
Code :
object = msc.connect(
host= ‘localhost’ ,
username= ‘root’ ,
password= ‘12345678’ ,
database= ‘test’)
cursor = object.cursor()
cursor.execute(“Select * From Student”)
For a in range(0,3):
records = cursor.fetchone()
print(records)
Ans 16
Code :
object = msc.connect(
host= ‘localhost’ ,
username= ‘root’ ,
password= ‘12345678’ ,
database= ‘test’)
cursor = object.cursor()
cursor.execute(“Update Student set marks= marks+5)
object.commit()
cursor.execute(“Select * From Student”)
For a = 1:
records = cursor.fetchone()
print(records)
Q 17) Write a Python program that displays all the rows fetched from
the student table of mysql database "test".
Ans 17
Code :
object = msc.connect(
host= ‘localhost’ ,
username= ‘root’ ,
password= ‘12345678’ ,
database= ‘test’)
cursor = object.cursor()
cursor.execute(“Select * From Student”)
Records=cursor.fetchall()
For a in Records:
Print(a)
Ans 18
Code :
Run = ‘try’
While Run== ‘try’:
Stack=[]
a = int(input(‘Choose an option/n1 Push /n2 Pop /n Display’))
If a == 1:
ele = eval(input(‘Enter the element to push ’))
Stack.append(ele)
elif a == 2:
if len(Stack)>0:
Stack.pop()
else:
print(‘Underflow!’)
elif a == 3:
L = len(Stack)
dis = Stack[::-1]
for k in dis:
print(k)
else :
Run=input(‘Invalid option chosen to continue type try’)
Ans 19
Code :
Hotel=[]
def Push_Cust() :
if list1[1] == ‘Deluxe' :
Hotel=[list1[0]] + Hotel
else:
pass
def Pop_Cust() :
if len(Hotel) > 0:
print(Hotel.pop(0))
else:
print(‘UNDERFLOW')
The program should then use the function Push3_5() to create the stack of
the list Only3_5. Thereafter pop each integer from the list Only3_5 and
display the popped value. When the list is empty, display the message
"StackEmpty''.
Ans 20
Code :
def Only3_5(N):
stk=[]
for a in N:
if a%5 == 0 or a%3 == 0:
stk.append(a)
else:
pass
print(‘stack is’,stk)
Num=[]
for a in range(0,5):
K=eval(input(‘Enter a number’))
Num+[K]
Only3_5(Num)
For a in range(0,6):
if len(stk)>0:
print(stk.pop())
else:
print(‘StackEmpty’)