CS PROGRAM FILE (SOLVED)
CS PROGRAM FILE (SOLVED)
PYTHON PROGRAMS
for a in line:
if a.islower():
lowercount += 1
elif a.isupper():
uppercount += 1
elif a.isdigit():
digitcount += 1
n = len(alist)
for i in range(n):
for j in range(0, n - i - 1):
if alist[j] > alist[j + 1]:
# Swap the elements
alist[j], alist[j + 1] = alist[j + 1], alist[j]
Output:
Output:
Output:
l = []
lr = []
a = []
n = int(input("Enter the number of elements in the list: "))
for i in a:
if len(i) > max1:
max1 = len(i)
temp = i
for i in range(3):
print("Random number generated is:", rand(a, b))
Output:
# Factorial function
def fac(x):
f=1
for i in range(1, x + 1):
f *= i
return f
# Example usage
print(sin(pi / 2, 5))
Output:
1.0045248555348174
# PROGRAM 10: MINIMUM ONE’S DIGIT IN TWO NUMBERS.
def comp(a, b):
if a1 < b1:
return a
elif b1 < a1:
return b
else:
return 0
# CODE :-
f=open("poem.txt","r")
l=f.readlines()
for i in l:
print(i)
Output:
Where the mind is without fear and the head is held high
Where the clear stream of reason has not lost its way
#code
text = open("poem.txt",'r')
word1="to"
word2="the"
c1=0
c2=0
for line in text:
words=line.split()
l=len(words)
for i in range(0,l):
s=words[i]
if s==word1:
c1=c1+1
if s==word2:
c2=c2+1
print("The word 'to' occurs",c1,"times")
print("The word 'the' occurs",c2,"times")
# OUTPUT:-
# Loop through the list from the end (most significant digit to
least significant)
for i in range(len(b_num)):
digit = b_num.pop() # Remove and get the last element (from
the right)
if digit == '1':
value = value + pow(2, i) # Add the corresponding power of 2
to the value
Output:
def octalToDecimal(n):
num = n
dec_value = 0
base = 1
temp = num
while temp:
last_digit = temp % 10
temp = int(temp / 10)
dec_value += last_digit * base
base = base * 8
return dec_value
# Example usage
num = 45621
print(octalToDecimal(num))
Output:
19345
# PROGRAM 15: TO WRITE DATA TO CSV FILE.
import csv
Output:
# PROGRAM 16:-BINARY SEARCH IN AN ARRAY.
for i in range(n):
ar[i] = int(input(f"Element {i}: ")) # Input elements into the list
# Output result
if ind != -1:
print(f"Element found at index {ind}")
else:
print("Element not found!")
Output:
import pickle
# Employee dictionaries
emp1 = {'Empno': 1201, 'Name': 'anushree', 'Age': 25, 'Salary':
47000}
emp2 = {'Empno': 1211, 'Name': 'zoya', 'Age': 30, 'Salary': 48000}
emp3 = {'Empno': 1251, 'Name': 'alex', 'Age': 31, 'Salary': 50000}
def isempty(stk):
# Check if the stack is empty
if stk == []:
return True
else:
return False
def pop(stk):
# Pop an item from the stack
if isempty(stk):
print("Underflow! Stack is empty.")
else:
item = stk.pop()
return item
def peek(stk):
# Peek at the topmost item of the stack
if isempty(stk):
print("Underflow! Stack is empty.")
else:
return stk[-1]
def display(stk):
# Display the stack
if isempty(stk):
print("Stack Empty")
else:
print("Stack (top -> bottom):")
for item in reversed(stk):
print(item)
stack = []
while True:
print("\nStack Operations")
print("1. Push")
print("2. Pop")
print("3. Peek")
print("4. Display Stack")
print("5. Exit")
if ch == 1:
item = int(input("Enter item to push: "))
push(stack, item)
elif ch == 2:
item = pop(stack)
if item is not None:
print("Popped item is:", item)
elif ch == 3:
item = peek(stack)
if item is not None:
print("Topmost item is:", item)
elif ch == 4:
display(stack)
elif ch == 5:
break
else:
print("Invalid choice!")
Output:
Stack Operations
1. Push
2. Pop
3. Peek
4. Display Stack
5. Exit
Enter your choice: 1
Enter item to push: 6
Stack Operations
1. Push
2. Pop
3. Peek
4. Display Stack
5. Exit
Enter your choice: 1
Enter item to push: 2
Stack Operations
1. Push
2. Pop
3. Peek
4. Display Stack
5. Exit
Enter your choice: 1
Enter item to push: 4
Stack Operations
1. Push
2. Pop
3. Peek
4. Display Stack
5. Exit
Enter your choice: 3
Topmost item is: 4
Stack Operations
1. Push
2. Pop
3. Peek
4. Display Stack
5. Exit
Enter your choice: 4
Stack (top -> bottom):
4
2
6
Stack Operations
1. Push
2. Pop
3. Peek
4. Display Stack
5. Exit
Enter your choice: 2
Popped item is: 4
Stack Operations
1. Push
2. Pop
3. Peek
4. Display Stack
5. Exit
Enter your choice: 4
Stack (top -> bottom):
2
6
Stack Operations
1. Push
2. Pop
3. Peek
4. Display Stack
5. Exit
Enter your choice: 5
# PROGRAM 19:- TO MODIFY THE CONTENTS OF BINARY FILE.
def update_binary(word, new):
# Get the word to be replaced and the new word from user input
word = input("Enter the word to be replaced: ").encode() #
Convert to bytes
new = input("Enter the new word: ").encode() # Convert to bytes
update_binary(word, new)
Output:
TEXT FILE
# PROGRAM 20:- PROGRAM TO FIND THE SUM OF
GEOMETRIC PROGRESSION SERIES.
import math
# Input values
a = int(input("Please Enter First Number of a G.P Series: "))
n = int(input("Please Enter the Total Numbers in this G.P Series:
"))
r = int(input("Please Enter the Common Ratio: "))
Output:
SQL OUTPUT
# PROGRAM 22:- TO INTEGRATE MYSQL WITH PYTHON TO
SEARCH AN EMPLOYEE USING EMPID AND DISPLAY THE
RECORD IF PRESENT IN ALREADY EXISTING TABLE EMP, IF
NOT DISPLAY THE APPROPRIATE MESSAGE.
Output:
Output:
import mysql.connector
con = mysql.connector.connect(host='localhost', user='root',
password='A123456z', db='jinal')
a = con.cursor()
sql = "SELECT * FROM school"
a.execute(sql)
myresult = a.fetchall()
for result in myresult:
print(result)
a.close()
con.close()
Output: