Professional Documents
Culture Documents
CSPC212
CSPC212
CSPC212
[Date]
HARSHITA CHOPRA
CLASS- XII-B ROLL NO. 24
Q2-Write a python code to define a function to find the factorial of a given
number.
n=int(input("enter a number: "))
def fact(n):
s=1
i=1
while i<=n:
s=s*i
i=i+1
print("the factorial of a given number",n,"is: ",s)
fact(n)
OUTPUT-
Q3- Write a python code to define a function checkprime to check whether the
number is prime or not prime.
n=int(input("enter a number: "))
def checkprime(n):
for i in range(2,n):
if n%i == 0:
print("prime")
break
else:
print("not prime")
checkprime(n)
OUTPUT-
Q1- Write a python code to define a function list change which excepts a list A of
numbers, the function will replace even numbers by 10 and odd number by 5.
def listchange(numbers):
result = []
for num in numbers:
if num % 2 == 0:
result.append('#')
else:
result.append('*')
return result
numbers = input("Enter a list of numbers separated by spaces: ")
list = [int(num) for num in numbers.split()]
changed = listchange(list)
print("Original List:", list)
print("Changed List:", changed)
OUTPUT
Q6- Write a program that fills a list with numbers using randint.
from random import randint
def fill(L,limit,low,high):
for i in range(limit):
L.append(randint(low,high))
minimum=int(input("min"))
maximum=int(input("max"))
n=int(input("enter limit of numbers"))
a=[]
fill(a,n,minimum,maximum)
print(a)
OUTPUT
Q7- Write a program for basic calculations like addition subtraction etc.
def calc(n1,n2):
print("sum is",n1+n2)
print("difference is",n1-n2)
print("product is",n1*n2)
print("quotient is",n1/n2)
a=int(input("enter the no. 1"))
b=int(input("enter the no. 2"))
calc(a,b)
OUTPUT
Q8- Write a code to define a function to generate a table of a given number.
def table(n):
for i in range(1,11):
print(n,"X",i,"=",n*i)
a=int(input("enter the number"))
table(a)
OUTPUT
Q9- Write a code to generate an OTP using random module.
import random
r=' '
for i in range(6):
r+=str(random.randint(0,9))
print("your otp is",r)
OUTPUT
Q10- Write a program to pick 3 lucky winners. The registration numbers of the
ticket are 10 digits long.
import random
for i in range(3):
print(random.randrange(0,1000000000,3))
OUTPUT
Q11- Write a python program to implement all basic operations of stack.
s=[]
c="y"
while(c=="y"):
print("1. Push")
print("2. Pop")
print("3. Display")
choice=int(input("enter your choice: "))
if (choice==1):
a=input("enter any number: ")
s.append(a)
elif (choice==2):
if (s==1):
print("stack empty")
else:
print("deleted element is: ",s.pop())
elif (choice==3):
l=len(s)
for i in range(l-1,-1,-1):
print(s[i])
else:
print("wrong input")
c=input("Do you want to continue?")
if c=='n':
break
OUTPUT
Q12- Write a program to display unique vowels present in the given word using
stack.
vowels=['a','e','i','o','u']
word=input("enter the word to seaarch for vowels: ")
stack=[]
for letter in word:
if letter in vowels:
if letter not in stack:
stack.append(letter)
print(stack)
print("the number of differnt vowels present in",word,"is",len(stack))
OUTPUT
Q13- Write a program to create a stack called EMPLOYEE, to perform the basic
operations on stack using list.
employee=[]
c="y"
while(c=="y"):
print("1. Push")
print("2. Pop")
print("3. Display")
choice=int(input("enter your choice: "))
if (choice==1):
eid=input("enter employee number: ")
ename=input("enter employee name: ")
emp=(eid,ename)
employee.append(emp)
elif (choice==2):
if (employee==1):
print("stack empty")
else:
eid,ename=employee.pop()
print("deleted element is: ",eid,ename)
elif (choice==3):
i=len(employee)
while i>0:
print(employee[i-1])
i=i-1
else:
print("wrong input")
c=input("Do you want to continue?")
if c=='n':
break
OUTPUT
Q14- Write a program in python to add, delete and display elements from a
queue using list.
a=[]
c="y"
while(c=="y"):
print("1. Insert")
print("2. Delete")
print("3. Display")
choice=int(input("enter your choice: "))
if (choice==1):
b=input("enter new number: ")
b.append(b)
elif (choice==2):
if (employee==1):
print("queue empty")
else:
print("deleted element is: ",a[0])
a.pop(0)
elif (choice==3):
l=len(a)
for i in range(0,1):
print(a[i]
else:
print("wrong input")
c=input("Do you want to continue?")
if c=='n':
break
OUTPUT
Q15- Write a program to perform basic operation of queue on details of books
like adding book name and book id in a queue.
library=[]
c="y"
while(c=="y"):
print("1. Insert")
print("2. Delete")
print("3. Display")
choice=int(input("enter your choice: "))
if (choice==1):
bid=input("enter book number: ")
bname=input("enter name of book: ")
lib=(bid,bname)
library.append(lib)
elif (choice==2):
if (library==1):
print("queue empty")
else:
print("deleted element is: ",library.pop(0))
elif (choice==3):
l=len(library)
for i in range(0,l):
print(library[i])
else:
print("wrong input")
c=input("Do you want to continue?")
if c=='n':
break
OUTPUT
Q16-Write a program to write the content of a binary file.
import pickle
def write():
f= open("myfile.dat",'wb')
l=['ram','shyam','sita','gita']
pickle.dump(l,f)
f.close()
write()
OUTPUT
update() .
OUTPUT