Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
12 views15 pages

CS Practical Program Codes Final

Uploaded by

jaydenmeinrad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views15 pages

CS Practical Program Codes Final

Uploaded by

jaydenmeinrad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

S.No Program Page No.

1. Fibonacci series 2

2. Mathematical Functions 3

3. Menu Driven Program(Arithmetic Operations) 4

4. Menu Driven Program(Factorial & Sum) 6

5. CSV file by entering user-id and password, read and search 7


the password for given userid

6. CSV file for employee details 8

7. Search and update a record 9

8. CSV file ‘record.csv’ and COUNTR() 11

9. Menu driven program using List(STACK) 12

10. Create Stack using Dictionary 14

1
Program No: 1

Aim: To develop a Fibonacci series python program

Python Code:
def fibonacci(n):
a, b = 0, 1
for i in range(n):
print(a, end=" ")
a, b = b, a + b
n = int(input("Enter the number of terms for Fibonacci series: "))
fibonacci(n)

Output:

2
Program No: 2

Aim: To develop a Mathematical functions python program

Python Code:
import math
def square(x):
return x * x
def cube(x):
return x * x * x
def square_root(x):
return math.sqrt(x)
num = float(input("Enter a number: "))
print("Square:", square(num))
print("Cube:", cube(num))
print("Square Root:", square_root(num))
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
if y != 0:
return x / y
else:
return "Cannot divide by zero"

Output:

Program No: 3
3
Aim: To develop a python program to create a menu driven program using user defined
functions to perform arithmetic operations

Python Code:
def add(a, b):
return a + b

def subtract(a, b):


return a - b

def multiply(a, b):


return a * b

def divide(a, b):


if b != 0:
return a / b
else:
return "Cannot divide by zero"

def menu():
print("Select an operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
choice = int(input("Enter choice (1/2/3/4): "))
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == 1:
print("Result: " + str(add(num1, num2)))
elif choice == 2:
print("Result: " + str(subtract(num1, num2)))
elif choice == 3:
print("Result: " + str(multiply(num1, num2)))
elif choice == 4:
print("Result: " + str(divide(num1, num2)))
else:
print("Invalid choice")

menu()
Output:

4
Program No: 4
5
Aim: To develop a python program to create a menu driven program using user defined
functions to find factorial and sum of list of numbers

Python Code:
def factorial(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
def sum_of_list(numbers):
total = 0
for number in numbers:
total += number
return total
def menu():
print("Select an operation:")
print("1. Factorial")
print("2. Sum of List of Numbers")
choice = int(input("Enter choice (1/2): "))
if choice == 1:
num = int(input("Enter number to find factorial: "))
print("Factorial:", factorial(num))
elif choice == 2:
nums = list(map(int, input("Enter numbers separated by space: ").split()))
print("Sum of numbers:", sum_of_list(nums))
else:
print("Invalid choice")
menu()

Output:

Program No: 1 [CSV]

6
Aim: To develop a python program to create a CSV file by entering user-id and password,
read and search the password for given userid

Python Code:
import csv

def create_user_csv():
with open('users.csv', mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["User ID", "Password"])
while True:
user_id = input("Enter User ID (or type exit to stop): ")
if user_id.lower() == "exit":
break
password = input("Enter Password: ")
writer.writerow([user_id, password])

def search_password(user_id):
with open('users.csv', mode='r') as file:
reader = csv.reader(file)
next(reader)
for row in reader:
if row[0] == user_id:
return row[1]
return "User ID not found"

create_user_csv()
search_id = input("Enter User ID to search for password: ")
password = search_password(search_id)
print("Password for " + search_id + ": " + password)
Output:

Program No: 2 [CSV]

7
Aim: To develop a python program to create a csv file for employee details with the
following fields [eno,ename,doj,desgn,basic,da,hra] append and calculate the gross pay

Python Code:
import csv
def create_employee():
with open('employee.csv', mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["E No", "E Name", "DOJ", "Designation", "Basic", "DA", "HRA",
"Gross Pay"])
while True:
eno = input("Enter Employee Number (or 'exit' to stop): ")
if eno.lower() == 'exit':
break
ename = input("Enter Employee Name: ")
doj = input("Enter Date of Joining: ")
desgn = input("Enter Designation: ")
basic = float(input("Enter Basic Salary: "))
da = float(input("Enter DA: "))
hra = float(input("Enter HRA: "))
gross_pay = basic + da + hra
writer.writerow([eno, ename, doj, desgn, basic, da, hra, gross_pay])
def display_employee():
with open('employee.csv', mode='r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
create_employee()
display_employee()

Output:

Program No: 3 [CSV]

8
Aim: To develop a python program to create csv file for employee details with the following
fields [eno,ename, doj,desgn,basic,da,hra ] search and update a record

Python Code:
import csv
def create_employee():
with open('employee.csv', mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["E No", "E Name", "DOJ", "Designation", "Basic", "DA", "HRA", "Gross
Pay"])
while True:
eno = input("Enter Employee Number (or 'exit' to stop): ")
if eno.lower() == 'exit':
break
ename = input("Enter Employee Name: ")
doj = input("Enter Date of Joining: ")
desgn = input("Enter Designation: ")
basic = float(input("Enter Basic Salary: "))
da = float(input("Enter DA: "))
hra = float(input("Enter HRA: "))
gross_pay = basic + da + hra
writer.writerow([eno, ename, doj, desgn, basic, da, hra, gross_pay])
def search_and_update():
neno = input("Enter Employee Number to search: ")
with open('employee.csv', mode='r') as file:
rows = list(csv.reader(file))
updated = False
for row in rows:
if row[0] == neno:
print("Current record: ", row)
row[1] = input("Enter new Employee Name: ")
row[2] = input("Enter new Date of Joining: ")
row[3] = input("Enter new Designation: ")
row[4] = float(input("Enter new Basic Salary: "))
row[5] = float(input("Enter new DA: "))
row[6] = float(input("Enter new HRA: "))
row[7] = row[4] + row[5] + row[6]
updated = True
break
if updated:
with open('employee.csv', mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerows(rows)
print("Employee record updated successfully!")
else:
print("Employee number not found.")
create_employee()
search_and_update()

Output:

9
Program No: 4 [CSV]

10
Aim: To develop a python program to create a CSV file ‘record.csv’ and COUNTR(): To
count the number of records present in the CSV file named ‘record.csv’

Python Code:
import csv
def count_records():
with open('record.csv', mode='r') as file:
reader = csv.reader(file)
rows = list(reader)
return len(rows) - 1
def create_sample():
n = int(input("Enter number of records: "))
with open('record.csv', mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["ID", "Name", "Age"])
for i in range(n):
id_val = input("Enter ID for record " + str(i + 1) + ": ")
name = input("Enter Name for record " + str(i + 1) + ": ")
age = input("Enter Age for record " + str(i + 1) + ": ")
writer.writerow([id_val, name, age])
create_sample()
record_count = count_records()
print("Number of records in 'record.csv': " + str(record_count))

Output:

Program No: 1 [STACK]

11
Aim: To develop a python program to create a menu driven program using List

Python Code:
stack = []
def push():
top = len(stack) - 1
element = input("Enter element to push: ")
stack.append(element)
print("Element pushed.")
def pop():
top = len(stack) - 1
if len(stack) == 0:
print("Stack is empty.")
else:
removed = stack.pop()
print("Popped element: " + str(removed))
def peek():
top = len(stack) - 1
if len(stack) == 0:
print("Stack is empty.")
else:
print("Top element: " + str(stack[top]))
def display():
top = len(stack) - 1
if len(stack) == 0:
print("Stack is empty.")
else:
print("Stack contents: " + str(stack))
def menu():
while True:
print("\nSelect an operation:")
print("1. Push")
print("2. Pop")
print("3. Peek")
print("4. Display Stack")
print("5. Exit")
choice = input("Enter choice (1/2/3/4/5): ")
if choice == "1":
push()
elif choice == "2":
pop()
elif choice == "3":
peek()
elif choice == "4":
display()
elif choice == "5":
print("Exiting program.")
break
else:
print("Invalid choice.")

12
menu()

Output:

Program No: 2 [STACK]

Aim: To develop a python program to create Stack using Dictionary

13
Python Code:
stack = {}
top = -1
def push():
global top
element = input("Enter element to push: ")
top = top + 1
stack[top] = element
print("Element pushed.")
def pop():
global top
if top == -1:
print("Stack is empty.")
else:
removed = stack[top]
del stack[top]
top = top - 1
print("Popped element: " + str(removed))
def peek():
if top == -1:
print("Stack is empty.")
else:
print("Top element: " + str(stack[top]))
def display():
if top == -1:
print("Stack is empty.")
else:
print("Stack contents:")
for i in range(top, -1, -1):
print(str(stack[i]))
def menu():
while True:
print("\nSelect an operation:")
print("1. Push")
print("2. Pop")
print("3. Peek")
print("4. Display Stack")
print("5. Exit")
choice = input("Enter choice (1/2/3/4/5): ")
if choice == "1":
push()
elif choice == "2":
pop()
elif choice == "3":
peek()
elif choice == "4":
display()
elif choice == "5":
print("Exiting program.")

14
break
else:
print("Invalid choice.")
menu()

Output:

15

You might also like