Advanced Programming Practice Lab: (Code 18CSC207J) B.Tech (CSE) - 2 Name

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

ADVANCED PROGRAMMING PRACTICE LAB

(Code 18CSC207J)

B.Tech (CSE) – 2nd year/4th Semester

Name:
Registration no.:

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

FACULTY OF ENGINEERING & TECHNOLOGY

SRM INSTITUTE OF SCIENCE & TECHNOLOGY,

DELHI NCR CAMPUS, MODINAGAR

Even Semester (2022-2023)


BONAFIDE CERTIFICATE

Registration no.

Certified to be the bonafide record of work done ___________ of 4th semester

2nd year B. TECH degree course in SRM INSTITUTE OF SCIENCE AND

TECHNOLOGY, NCR Campus of Department of Computer Science &

Engineering, in ADVANCED PROGRAMMING PRACTICE, during the

academic year 2022-2023.

Lab In charge Head of the Department (CSE)

Submitted for university examination held on __/____/___ at SRM IST, NCR


Campus.
LAB EXPERIMENT – 4
Event Driven Programming

Practical 1:- Write a Python program to draw square using Turtle Programming

Code :-
import turtle
skk = turtle.Turtle()
for i in range(4):
skk.forward(50)
skk.right(90)
turtle.done()

Output :-
Practical 2:- Write a python program to show basic tkinter program.

Code :-
from tkinter import *
win = Tk()
win.geometry("200x200")
w = Label(win, text="Hello, Welcome to python")
w.pack()
win.mainloop()

Output :-
LAB EXPERIMENT – 5
Declarative Programming

Practical 1:- To perform a program using array

Code :-
import numpy as np
a = np.array([[1,2,3],[12,31,2]])
print("array 1",a)
a = np.array([[1,2,3],[9,21,5]])
print("array2",a)
a = np.array((1,3,2))
print("The tuple array",a)

Output :-

Practical 2:- To perform various functions to an array like adding, subtracting etc
Code :-
import numpy as np
a = np.array([[1,2,3],[12,31,2]])
print("array 1",a)
b = np.array([[1,5,8],[9,21,5]])
print("array2",b)
print("Add 1",a + 1)
print("sub 1",a - 1)
print("Mul 2", a*2)
print("divide 2", a/2)
print("Add 2 array", a + b)
print("subtract 2 array", a - b)
print("mutiply 2 array", a * b)
print("divide 2 array", a / b)
Output :-
LAB EXPERIMENT – 6
Imperative Programming

Practical 1:- Find the number is Armstrong or not.

Code :-
a = int(input("Enter the number "))
t=a
s=0
for i in range(3):
d = t%10
s = s+ d**3
t=t/10
if s == a:
print("An Armstrong number")
else:
print("Not an armstrong number")
Output :-
Practical 2:- Find the number is prime or not.
Code :-
a = int(input("Enter the number "))
if a>1:
for i in range(2,a):
if a%i==0:
print("The number is not a prime number")
break
if i==(a -1):
print("The number is a prime number")

Output :-
LAB EXPERIMENT – 7
Parallel Programming

Practical 1:- To make child Threads.

Code :-
from threading import *
def display() :
for i in range(3) :
print("Child Thread")
Thread_obj = Thread(target=display)
Thread_obj.start()
for i in range(3):
print('Main Thread')

Output :-
Practical 2:- Create Threads using class.
Code :-
from threading import Thread,current_thread
class AB(Thread):
def ren(self):
for i in range(7):
print("child")
a = AB()
a.start()
a.join()
print("Bye from",current_thread().getName())

Output :-
LAB EXPERIMENT – 8
Concurrent Programming

Practical 1:- To make a Child Thread.

Code :-
import threading
import time
class AB(threading.Thread):
def run(self):
for i in range(6):
print("child")
a = AB()
a.start()
time.sleep(1)
print("Bye ")

Output :-

Practical 2:- Use a Semaphore.


Code :-
from threading import *
import time
obj = Semaphore(2)
def display(name):
obj.acquire()
for i in range(2):
print('Hello ,', end = '')
time.sleep(1)
print(name)
obj.release()
t1 = Thread(target = display , args = ('Thread-1',))
t2 = Thread(target = display , args = ('Thread-2',))
t3 = Thread(target = display , args = ('Thread-3',))
t4 = Thread(target = display , args = ('Thread-4',))
t5 = Thread(target = display , args = ('Thread-5',))
t1.start()
t2.start()
t3.start()
t4.start()
t5.start()
Output :-
LAB EXPERIMENT – 9
Functional Programming

Practical 1:- Use of Lambda Function.

Code :-
double = lambda x : x*5
print (double(5))

Output :-

Practical 2:- Use of Map Function


Code :-
a = [1,2,3,4,5]
mapl = list(map(lambda b:b*2,a))
print(mapl)
Output :-
LAB EXPERIMENT – 10
Logical Programming

Practical 1:- Use of Pydatalog .

Code :-
from pyDatalog import pyDatalog
pyDatalog.create_terms('big,small,brown,black,grey,dark,X,Y,Z')
+big('bear')
+big('elephant')
+small('cat')
+brown('bear')
+black('cat')
+grey('elephant')
dark(X)<=black(X)
dark(X)<=brown(X)
print(dark(X))
print('The smallest animal')
print(small(X))
Output :-

Practical 2:- Use Pydatalog .


Code :-
from pyDatalog import pyDatalog
pyDatalog.create_terms('parent,male,female,son,daughter,X,Y,Z')
+male('adam')
+male('james')
+female('anne')
+female('barney')
+parent('barney','adam')
+parent('james','anne')
son(X,Y)<=male(X) & parent(Y,X)
daughter(X,Y)<=parent(Y,X) & female(X)
print(pyDatalog.ask('son(adam,Y)'))
print(pyDatalog.ask('daughter(anne,Y)'))
print(son('adam',X))
Output :-
LAB EXPERIMENT – 11
Declarative Type Programming

Practical 1:- Pattern Printing.

Code :-
for i in range(1,6):
for j in range(1,6-i):
print(" ",end=" ")
for k in range(1,(2*i)):
print("*",end=" ")
print("\n")
Output :-

Practical 2:- Pattern Printing


Code :-
for i in range(1,6):
for k in range(1,6-i):
print(" ",end=" ")
for j in range(1,i+1):
print("*",end=" ")
print("\n")

Output :-
LAB EXPERIMENT – 12
Network Programming

Practical 1:-Creating a socket.

Code :-
import socket
import sys
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Socket successfully created")
except socket.error as err:
print ("Socket creation failed with error %s"%(err))
port = 80
Output :-

PRACTICAL 2: WAP to create a socket

CODE:-

import socket

try:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

except socket.error as err:

print ("Socket creation failed with error %s"%(err))

host = socket.gethostname()

port = 12345

s.connect((host, port))
print ( s.recv(1024) )

s.close() OUTPUT:
LAB EXPERIMENT – 13
Symbolic Programming

Practical 1:- The evaluation of expression.

Code :-
import sympy as sym
x, y = sym.symbols('x y')
e = x+2*y
print(e)

Output :-

Practical 2:- The evaluation of expression.


Code :-
import sympy as sym
x, y = sym.symbols('x y')
a = sym.expand((x + y)**6)
b= sym.simplify((x+x * y)/x)
d= sym.series(sym.cos(x),x)
print(d)

Output :-
LAB EXPERIMENT – 14
Automata Programming

Practical 1:- Use of Automata.

Code :-
from automata.fa.dfa import DFA
dfa = DFA(
states={'q0','q1','q2','d'},
input_symbols ={'0','1'},
transitions={
'q0':{'0':'d','1':'q1'},
'q1':{'0':'q2','1':'q1'},
'q2':{'0':'q2','1':'q1'},
'd':{'0':'d','1':'d'}

},
initial_state='q0' ,
final_states={'q2'}
)
if(dfa.accepts_input("1000")):
print("Accepted")
else:
print("Rejected")
Output :-
PRACTICAL 2: WAP for a language that accepts all strings starting with 01
OR ending with 01

TRANSITION TABLE: STATE DIAGRAM:

Q/E 0 1

→A B D

B E C

C* C C

D E D

E E F

F* E D

CODE:

%pip install automata-lib from


automata.fa.dfa import DFA dfa =
DFA(
states={'A’,’B’,’C’,’D’,’E’,’F’},
input_symbols ={'0','1'},
transitions={
'A':{'0':'B','1':'D'},
'B':{'0':'E','1':'C'},
'C':{'0':'C','1':'C'},
'D':{'0':'E','1':'D'},
'E':{'0':'E','1':'F'},
'F':{'0':'E','1':'D'}
},
initial_state='A' ,
final_states={'C',’F’}
)
str1=input("Enter input string- ")
if(dfa.accepts_input(str1)):
print("Accepted")
else: print("Rejected")

OUTPUT:
Accepted
Lab 15: Graphical User Interface (GUI) Programming
Practical 1: Creating a form using tkinter
import tkinter
as tk from
tkinter import
* win = tk.Tk()
win.title("Registration Form") win.geometry('500x500')
bg = PhotoImage(file = 'C:\harshita\photo\sky.png') bg =
bg.subsample(5,5)
label1 = tk.Label( win, image = bg).place(x = 0,y = 0)

a= tk.Label(win ,text= "Full Name",font="Arial",


bg="orange").place(x=68, y=30)

b = tk.Label(win ,text= "E mail",font="Arial", bg="orange").place(x=70, y=80)


c = tk.Label(win ,text= "Gender",font="Arial", bg="orange").place(x=70, y=130)
d = tk.Label(win ,text= "Age",font="Arial", bg="orange").place(x=70, y=180)
e= tk.Label(win ,text= "password",font="Arial", bg="orange").place(x=70,
y=230)

f = tk.Entry(win , bg="orange").place(x=240, y=30)


g = tk.Entry(win , bg="orange").place(x=240, y=80) h = tk.Entry(win ,
bg="orange").place(x=240, y=180) i = tk.Entry(win , show='*' ,
bg="orange").place(x=240, y=230) var=IntVar()
Radiobutton(win,text="Male",padx= 5, variable= var, value=1 ,
bg="orange").place(x=240,y=130)

Radiobutton(win,text="Female",padx= 20, variable= var, value=2 ,


bg="orange").place(x=295,y=130) def open_window(): top = Toplevel()
top.title("Registration Form") top.geometry("300x300")
label5 = Label(top,text= "Registration Done",font="Arial",
bg="orange").grid(row= 2, column= 5)

photo = PhotoImage(file = 'C:\harshita\photo\circle_PNG26.png' ) photoimage =


photo.subsample(40,60)
Button(win, text = 'Submit', image = photoimage, compound= LEFT
,command= open_window).place(x=350, y=280) win.mainloop()
Output:

You might also like