First Two PLC Lab Programs Eee - Ec
First Two PLC Lab Programs Eee - Ec
LAB PROGRAMS
# 1a. Develop a program to read the student details like Name, USN, and Marks
in three subjects. Display the student details, total marks, and percentage
with suitable messages.
➢ Output
# 1b. Develop a program to read the name and year of birth of a
person. Display whether the person is a senior citizen or not.
➢ Output:
# 2a. Develop a program to generate Fibonacci sequence of length (N). Read N
from the console.
def generate_fibonacci_sequence(length):
sequence = [0, 1]
for i in range(2, length):
next_number = sequence[-1] + sequence[-2]
sequence.append(next_number)
return sequence
def main():
try:
N = int(input("Enter the length of the Fibonacci sequence: "))
if N <= 0:
print("Length should be a positive integer.")
else:
fibonacci_sequence = generate_fibonacci_sequence(N)
print("Fibonacci sequence of length", N, ":", fibonacci_sequence)
except ValueError:
print("Please enter a valid integer for length.")
if __name__ == "__main__":
main()
➢ Output:
def fact(num):
fact = 1
for i in range(num, 0, -1):
fact = fact * i
return factN = int(input("Enter the value for N: ")
R = int(input("Enter the value for R: "))
NF = fact(N)
RF = fact(R)
print("Factorial of " + str(N) + " is", NF)
BMC = NF / (RF * NRF)
print("Binomial coefficient is", BMC)
➢ Output:
# 3. Read N numbers from the console and create a list. Develop a program to
print mean, variance and standard deviation with suitable messages.
import math
n = int(input('Enter the number of elements:'))
lst = []
for i in range(n):
num = float(input('Enter number ' + str(i + 1) + ':'))
lst.append(num)
mean = sum(lst) / n
k = 0
for x in lst:
k = k + ((x - mean) ** 2)
variance = k / n
std_dev = math.sqrt(variance)
print('The list is', lst)
print('The mean of the list is:', mean)
print('The variance of the list is:', variance)
print('The standard Deviation of the list is:', std_dev)
➢ Output:
# 4. Read a multi-digit number (as chars) from the console. Develop a program
to print the frequency of each digit with suitable message.
➢ Output:
import sys
import string
import os.path
fname = input("Enter the filename : ") #sample file text.txt also provided
if not os.path.isfile(fname):
print("File", fname, "doesn't exists")
sys.exit(0)
infile = open(fname, "r")
filecontents = ""
for line in infile:
for ch in line:
if ch not in string.punctuation:
filecontents = filecontents + ch
else:
filecontents = filecontents + ' '
wordFreq = {}
wordList = filecontents.split()
#Calculate word Frequency
for word in wordList:
if word not in wordFreq.keys():
wordFreq[word] = 1
else:
wordFreq[word] += 1
➢ Output:
# 6. Develop a program to sort the contents of a text file and write the sorted
contents into a separate text file. [Hint: Use string methods strip(), len(),
list methods sort(), append(), and file methods open(), readlines(), and
write()].
import os.path
import os.sys
fname = input("Enter the filename whose contents are to be sorted : ")
#sample file unsorted.txt also provided
if not os.path.isfile(fname):
print("File", fname, "doesn't
exists") sys.exit(0)
myList =
infile.readlines()
# print(myList)
#Remove trailing \n
characters lineList = []
for line in myList:
lineList.append(line.str
ip())
lineList.sort()
outfile =
open("sorted.txt","w")
#
7.
# #
# #
# #
#
8.
# 8. Write a function named DivExp which takes TWO parameters a, b and
returns a value c (c=a/b). Write suitable assertion for a>0 in function DivExp
and raise an exception for when b=0. Develop a suitable program which reads two
values from the console and calls a function DivExp.
def DivExp(a,p):
assert a>0, “the a is less than 0”
if b==0:
raise Exception('b should not be equal to 0')
c=a/b
return c
try:
print("enter the a value")
a=int(input())
print ('enter the b values')
b=int(input())
print(DivExp(a,b))
except Exception as err:
print(err