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

Geethanjali Vidyalaya Class Xii Sub: Computer Science Lab Programs (For Record) Programs 1 To 10

The document provides instructions for computer science lab programs 1-10 for class 12. It includes sample Python code for basic arithmetic operations with functions, temperature conversion, Fibonacci series, factorials, login validation, menu-driven options for checking palindromes/Armstrong numbers, prime numbers between ranges, and patterns like diamond, butterfly and triangle using functions. It also includes functions to write data to a binary file and read/print contents of a CSV file in a proper formatted way.

Uploaded by

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

Geethanjali Vidyalaya Class Xii Sub: Computer Science Lab Programs (For Record) Programs 1 To 10

The document provides instructions for computer science lab programs 1-10 for class 12. It includes sample Python code for basic arithmetic operations with functions, temperature conversion, Fibonacci series, factorials, login validation, menu-driven options for checking palindromes/Armstrong numbers, prime numbers between ranges, and patterns like diamond, butterfly and triangle using functions. It also includes functions to write data to a binary file and read/print contents of a CSV file in a proper formatted way.

Uploaded by

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

GEETHANJALI VIDYALAYA

CLASS XII
SUB: COMPUTER SCIENCE
LAB PROGRAMS (FOR RECORD)
PROGRAMS 1 TO 10
Instructions:
➢ Write the question and Source code on the right hand side of the
record book.
➢ Output should be written in blank left hand side with pencil
➢ Submission date will be intimated shortly

1. Write a python program to perform the basic arithmetic operations in a menu-driven program
with different functions. The output should be like this:
Select an operator to perform the task:
‘+’ for Addition
‘-‘ for Subtraction
‘*’ for Multiplication
‘/’ for Division

def main():
print('+ for Addition')
print('- for Subtraction')
print('* for Multiplication')
print('/ for Division')
ch = input("Enter your choice:")
if ch=='+':
x=int(input("Enter value of a:"))
y=int(input("Enter value of b:"))
print("Addition:",add(x,y))
elif ch=='-':
x=int(input("Enter value of a:"))
y=int(input("Enter value of b:"))
print("Subtraction:",sub(x,y))
elif ch=='*':
x=int(input("Enter value of a:"))
y=int(input("Enter value of b:"))
print("Multiplication",mul(x,y))
elif ch=='/':
x=int(input("Enter value of a:"))
y=int(input("Enter value of b:"))
print("Division",div(x,y))
else:
print("Invalid character")
def add(a,b):
return a+b
def sub(a,b):
return a-b
def mul(a,b):
return a*b
def div(a,b):
return a/b
main()

2. Write a python program to enter a temperature in Celsius into Fahrenheit by using function.
def tempConvert():
cels = float(input("Enter temperature in celsius: "))
fh = (cels * 9/5) + 32
print('%.2f Celsius is: %0.2f Fahrenheit' %(cels, fh))
tempConvert()

3. Write a python program using a function to print Fibonacci series up to n numbers.


def fibo():
n=int(input("Enter the number:"))
a=0
b=1
temp=0
for i in range(0,n):
temp = a + b
b=a
a= temp
print(a, end=" ")
fibo()

4. Write a python program to return factorial series up to n numbers using a function.


def facto():
n=int(input("Enter the number:"))
f=1
for i in range(1,n+1):
f*=i
print(f, end=" ")
facto()

5. Write a python program to accept username “Admin” as default argument and password 123
entered by user to allow login into the system.
def user_pass(password,username="Admin"):
if password=='123':
print("You have logged into system")
else:
print("Password is incorrect!!!!!!")
password=input("Enter the password:")
user_pass(password)

6. Write menu-driven python program using different functions for the following menu: 1 Check no.
is Palindrome or not
2 Check no. is Armstrong or not
3 Exit
def checkPalin(n):
temp=n
rem=0
rev=0
while(n>0):
rem=n%10
rev=rev*10+rem
n=n//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number is not a palindrome!")
def checkArmstrong(n):
temp=n
rem=0
arm=0
while(n>0):
rem=n%10
arm+=rem**3
n=n//10
if(temp==arm):
print("The number is an armstrong!")
else:
print("The number is not an armstrong!")
def menu():
print("1.Check no. is Palindrome:")
print("2.Check no. is Armstrong:")
print("3.Exit")
opt=int(input("Enter option:"))
no=int(input("Enter number to check:"))
if opt==1:
checkPalin(no)
elif opt==2:
checkArmstrong(no)
elif opt==3:
sys.exit()
else:
print("Invalid option")
menu()

7. Write a python program using a function to print prime numbers between 11 to 200.
start =11
end =200
print("Prime numbers between", start, "and", end, "are:")
for n in range(start, end + 1):
if n > 1:
for i in range(2, n):
if (n % i) == 0:
break
else:

print(n,",",end=" ")

8. Write a python program to print the following patterns using functions: 1. Diamond Pattern with *
2. Butterfly Pattern with *
3. Triangle Pattern with *
def pattern_diamond(n):
no = 0
for i in range(1, n + 1):
for j in range (1, (n - i) + 1):
print(end = " ")
while no != (2 * i - 1):
print("*", end = "")
no = no + 1
no = 0
print()
k=1
no = 1
for i in range(1, n):
for j in range (1, k + 1):
print(end = " ")
k=k+1
while no <= (2 * (n - i) - 1):
print("*", end = "")
no = no + 1
no = 1
print()
num=int(input("Enter no or lines to print:"))
pattern_diamond(num)

def pattern_butterfly(n):
for i in range(1, n + 1):
for j in range(1, 2 * n + 1):
if (i < j):
print("", end = " ");
else:
print("*", end = "");
if (i <= ((2 * n) - j)):
print("", end = " ");
else:
print("*", end = "");
print("");
for i in range(1, n + 1):
for j in range(1, 2 * n + 1):
if (i > (n - j + 1)):
print("", end = " ");
else:
print("*", end = "");
if ((i + n) > j):
print("", end = " ");
else:
print("*", end = "");
print("");
num=int(input("Enter no or lines to print:"))
pattern_butterfly(num);

def pattern_triangle(n):
for i in range(1, n+1):
for j in range(1, i+1):
print("* ",end="")
print("r")
num=int(input("Enter no or lines to print:"))
pattern_triangle(num)

9. Write a function to write data into binary file marks.dat and display the records of students who
scored more than 95 marks.
import pickle

def search_95plus():

f = open("marks.dat","ab")

while True:

rn=int(input("Enter the rollno:"))


sname=input("Enter the name:")

marks=int(input("Enter the marks:"))

rec=[]

data=[rn,sname,marks]

rec.append(data)

pickle.dump(rec,f)

ch=input("Wnat more records?Yes:")

if ch.lower() not in 'yes':

break

f.close()

f = open("marks.dat","rb")

cnt=0

try:

while True:

data = pickle.load(f)

for s in data:

if s[2]>95:

cnt+=1

print("Record:",cnt)

print("RollNO:",s[0])

print("Name:",s[1])

print("Marks:",s[2])

except Exception:

f.close()

search_95plus()
10. Read a CSV file top5.csv and print the contents in a proper format. The data for top5.csv file are
as following:
SNo Batsman Team Runs Highest

1 K L Rahul KXI 670 132*

2 S Dhawan DC 618 106*

3 David Warner SRH 548 85*

4 Shreyas Iyer DC 519 88*

5 Ishan Kishan MI 516 99

from csv import reader


def pro1():
with open(“top5.csv”,”r”) as f:
d = reader(f)
data=list(d)
for i in data:
print(i)
pro1()

You might also like