Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Computer Assignment

Name – Syed Mohammad Akbar Alam

Class – 12th Science A


Q 1) Program to print sum of natural numbers between 1 to 9. Print on
sum progressively ie, after adding each natural number print sum so
far.

Ans 1) Code:
summ=0
for a in range(1,10):
summ += a
print('The sum of ',a,'consecutive natural numbers is',summ)

Output:

The sum of 1 consecutive natural numbers is 1


The sum of 2 consecutive natural numbers is 3
The sum of 3 consecutive natural numbers is 6
The sum of 4 consecutive natural numbers is 10
The sum of 5 consecutive natural numbers is 15
The sum of 6 consecutive natural numbers is 21
The sum of 7 consecutive natural numbers is 28
The sum of 8 consecutive natural numbers is 36
The sum of 9 consecutive natural numbers is 45

Q 2) Program to calculate and print roots of the quadratic equations.


a * x ^ 2 + bx + c = 0

Ans 2
Code:

#equation: a*x^2 + b*x +c

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:

Enter the coefficient of x^2:1


Enter the coefficient of x:1
Enter the numerical term:-2
roots of equation are : 1.0 and -2.0

Q 3) Program to calculate factorial of a number.

Ans 3
Code:

a= int(input('enter a non-negative integer:\t'))


fact=1
if a==0:
print('The factorial of',a,'is',fact)
elif a<0:
print('not a valid input')
else:
for k in range(1,a+1):
fact*=k
print('The factorial of',a,'is',fact)

Output:
enter a non-negative integer: 5
The factorial of 5 is 120

enter a non-negative integer: -6


not a valid input

Q 4) Program to calculate simple interest using a function interest ()


that can receive principal amount, time and rate and returns
calculated simple interest. Do specify default values for rate and time
as 10% and 2 years respectively.
Ans 4)

Code:

def interest(p,r=10,t=2):
int_=p*r*t
print(int_)

Output:

interest(12,45,2)
1080

interest(2)
40

Q 5) Program that receives two numbers in a function and returns the


results of all arithmetic operations (+,-,*, /, %) on these numbers.

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

Q 6) Given the following Python code, which is repeated four times.


What could be the possible set of outputs out of given four sets (dddd
represent any combination of digits)?
Ans 6)
Import random

print(15+random.random()*5)

i. 17.dddd, 19.dddd, 20,dddd, 15.dddd


ii. 15.dddd, 17.dddd, 19.dddd, 18.dddd
iii. 14.dddd, 16.dddd, 18.dddd, 20.dddd

Ans 6

ii. 15.dddd, 17.dddd, 19.dddd, 18.dddd

Q 7) Read file stu.dat created in earlier program and display record


showing marks>18.
Ans 7
Code:

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 :

{‘Rollno’:12, ‘Name’: ‘AB’, ‘Marks’:34.8}

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:

{‘Rollno’:12, ‘Name’: ‘AB’, ‘Marks’:34.8}


{‘Rollno’:14, ‘Name’: ‘KL’, ‘Marks’:12.0}

Q 9) The data of winners of four rounds of a competitive programming


competition is given as:

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]

Q 11) Given the following student Relation Student.

Write SQL commands for (a) to (e).

(a) To show all information about the students of History department


(b) To list the names of female students who are in Hindi department
(c) To list names of all students with their date of admission in
ascending order.
(d) To display student's Name, Fee, Age for male Students only.
(e) To count the number of students with Age <23.
Ans 11
Code:

(a) Select * From Student


Where Department= ‘History’ ;
(b) Select Name From Student
Where Sex= ‘F’ and Department= ‘Hindi’ ;
(c) Select Name From Student
Order by Dateofadm asc ;
(d) Select Name,Fee,Age From Student
Where Sex= ‘M’ ;
(e) Select count(*) From Student
Where Age <23 ;

Q 12) Given the following tables for a database LIBRARY.


Write SQL queries for (a) to (e):

(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:

(a) Select Book_Name, Author_Name, Price From Books


Where Publishers = ‘First Publ.’ ;
(b) Select Book_Name From Books
Where Type = ‘Text’ ;
(c) Select Book_Name, Price From Books
Order by Price asc ;
(d) Update Books
Set Price = Price +50
Where Publishers= ‘EPB’ ;
(e) Select Book.Book_Id, Book_Name, Quantity_Issued
From Book,Issued
Where Book.Book_Id = Issued.Book_Id ;

Q 13) Consider the following DEPT and WORKER tables. Write SQL
queries for (i) to (iv).

(i) To display WNO, NAME, GENDER from the table Worker in


descending order of worker.
(ii) To display the Name of all the FEMALE workers from the table
WORKER.
(iii) To display the wno and name of those worker from the table
worker who are born between '1987-01-01' and '1991-12-01'
(iv) To count and display MALE workers who have joined after '1986-
01-01’

Ans 13
Code:

(a) Select WNO, NAME, GENDER From WORKER


Order by WNO desc ;
(b) Select NAME From WORKER
Where GENDER = ‘FEMALE’ ;
(c) Select WNO, NAME From WORKER
Where DOB Between '1987-01-01' and '1991-12-01’ ;
(d) Select Count(*) From WORKER
Where GENDER = ‘MALE’ and DOB >'1986- 01-01’ ;
Q 14) Write queries (a) to (d) based on the tables EMPLOYEE and
DEPARTMENT given below:

a) To display the average salary of all employees, department wise.


b) To display the name and respective department name of each
employee whose salary is more than 50000.
c) To display the names of employees whose salary is not known, in
alphabetical order.
d) To display DEPTID from the table EMPLOYEE without repetition.

Ans 14
Code :

(a) Select Avg(SALARY) From EMPLOYEE


Group by DEPTID ;
(b) Select NAME, DEPT NAME
From EMPLOYEE E, DEPARTMENT D
Where E.DEPTID = D.DEPTID
And SALARY > 50000 ;
(c) Select NAME From EMPLOYEE
Where SALARY is NULL
Order by NAME asc ;
(d) Select DISTINCT DEPTID From EMPLOYEE ;

Q 15) Write a Python program that displays first three rows fetched
from student table of MySQL database "test”

Ans 15
Code :

import mysql.connector as msc

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)

Q 16) Write a Python program that updates the record of a table


student and displays the rows fetched from the student table of mysql
database "test".

Ans 16
Code :

import mysql.connector as msc

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 :

import mysql.connector as msc

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)

Q 18) Python program to implement stack operations(ie Push, Pop,


Display stack).

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’)

Q 19) A list contains following record of customer:


[Customer_name, Room Type]
Write the following user defined functions to perform, given
operations on the stack named 'Hotel' :

(i) Push_ Cust () - To Push customers' names of those customers


who are staying in 'Deluxe' Room Type.
(ii) Pop_ Cust () - To Pop the names of customers from the stack
and display them. Also, display ''Underflow" when there are no
customers in the stack.

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')

Q 20) Write the definition of a user defined function Push3_5(N) which


accepts a list of integers in a parameter N and pushes all those
integers which are divisible by 3 or divisible by 5 from the list N into a
list named Only3_5.

Write a program in Python to input 5 integers into a list named NUM.

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’)

You might also like