You are on page 1of 64

ADVANCE

D
COMPUTER
SKILLS(ACS)
RECORD

NAME: BOMMA SRI MUKHI

ROLL NO: 2451-19-733-021

BRANCH: CSE

SECTION: 1
1
Page

1
2

Page 2
3

Page 3
1. Find distance between two points x1,x2,y1,y2 taking input from the user

#Code-1:

print("Enter x1,y1,x2,y2");
x1=float(input())
y1=float(input())
x2=float(input())
y2=float(input())
d=((x2-x1)*2+(y2-y1)2)*0.5
print("distance=%0.3f"%d)

#Code-2:

import math
print("Enter x1,y1,x2,y2");
x1=float(input())
y1=float(input())
x2=float(input())
y2=float(input())
d=math.sqrt(math.pow(x2-x1,2)+math.pow(y2-y1,2))
print("using math,distance=%0.3f"%d)
print("distance=",d)

Output:
Enter x1,y1,x2,y2
3.4
5.6
7.8
9.4
distance=8.200
4
Page

4
2. Read a set of numbers from the command line ,add & print those numbers

import sys
n=len(sys.argv)
print("Number of command line
arguments=",n) print("Program saved
as:",sys.argv[0]) print("Command line
arguments are:")
for i in range(1,n):
print(sys.argv[i])
sum=0
for i in range(1,n):
sum+=int(sys.argv[i])
print("Sum of arguments=",sum)

OUTPUT:
Number of command line
arguments=1 Program saved
as:main.py
Command line
arguments Sum of
arguments=0

5
Page
3. Determine a given number is even or odd

#code:

n=int(input("enter a number"))
if n%2==0:
print("even")
else:
print("odd")

OUTPUT:
Enter a number
25 odd

6
Page
4. Display two random numbers that are to be added,the program
should allow the student to enter the answer if the answer is correct a
message congratulations is to be printed ,if the answer is wrong the
correct answer should be displayed

#CODE

import random

n=int(input("enter n value"))

a=random.randint(1,n)
b=random.randint(1,n)

c=a+b

m=int(input("enter value"))

if c==m:
print("congratulations")
else:
print(c)

OUTPUT:

Enter n value 6

Enter m value4 Cogratulations


7
Page
5.compute sumof(i,n)1/n

#CODE

n=int(input("enter n value"))
sum=0
for i in
range(1,n+1):
sum=sum+(1/i)
print(sum)

OUTPUT:

Enter n value 8
2.717857142857145

8
Page
6. demonstrate the operations performed on lists,tupples sets and dictionaries

#CODE:

#list
mylist = ["apple", "banana",
"cherry"] print(mylist)

#can access them by referring to the index


number mylist[1] = "blackcurrant"
print(mylist)

#Using the append() method to append an item:


mylist.append("orange")
print(mylist)

#The remove() method removes the specified


item. mylist.remove("cherry")
print(mylist)

#Print all items in the list, one by


one for x in mylist:
print(x)

#tuple

mytuple = ("apple", "banana",


"cherry") print(mytuple)

#ccess tuple items by referring to the index


number print(mytuple[1])
print(mytuple)

#Once a tuple is created, you cannot add items to it


#mytuple.append("orange") # This will raise an
error

#Convert the tuple into a


list mynewlist =
9

list(mytuple) mynewlist[1] =
Page
"kiwi" mytuple =
tuple(mynewlist)
print(mytuple)

#You can loop through the tuple items by using a for


loop. for x in mytuple:
print(x)

#set
myset = {"apple", "banana",
"cherry"} print(myset)

#Loop through the set, and print the


values for x in myset:
print(x)

#Add an item to a set, using the add() method:


myset.add("orange")
print(myset)

#Remove "banana" by using the remove(),pop(),discard()


method myset.remove("banana")
print(myset)

"""
You can use the union() method that returns a new set containing all items from both sets,
or the update() method that inserts all the items from one set into another
"""
mynewset={"sapota","grapes","apple"}
resultset = myset.union(mynewset)
print(resultset)

#dictionary

mydictionary={"rollno:021","name:srimukhi","branch:CSE"}
print(mydictionary)
10
Page
#you can loop through the dictionary items by using a for loop
for val in mydictionary:
print(val)

OUTPUT:

[‘apple’,’banana’,’cherry’]
[‘apple’,’blackcurrant’,’cherry’]
[‘apple’,’blackcurrant’,’cherry’,’orange’]
[‘apple’,’blackcurrant’,’orange’]
apple
blackcurrant
orange
(‘apple’,’banana’,’cherry’)
banana
(‘apple’,’banana’,’cherry’)
(‘apple’,’kiwi’,’cherry’)
apple
kiwi
cherry
{‘cherry’,’banana’,’apple’}
cherry
banana
apple
{‘cherry’,’orange’,’banana’,’apple’}
{‘cherry’,’orange’,’apple’}
{‘grapes’,’cherry’,’orange’,’apple’,’sapota’}
{‘rollno:021’,’branch:CSE’,’name:srimukhi’}
rollno:021

branch:CSE
name:srimukhi

11
Page
7. Find the factorial of a number without recursion

#CODE:

def factorial(n):
f=1
for i in
range(1,n+1):
f=f*i
return f

n=int(input("Enter number:"))

print("Factorial of the number is:


") print(factorial(n))

OUTPUT:
Enter number:6
Factorial of the number is:720

12
Page
8. Generate fibonacci sequence upto the given number n using recursion

#CODE:

def fibonacci(n):
if(n <= 1):
return n
else:
return(fibonacci(n-1) + fibonacci(n-2))

n = int(input("Enter number of
terms:")) print("Fibonacci sequence:")
for i in range(n):
print(fibonacci(i),)

OUTPUT:
Enter the number of
terms:4 Fibonacci
sequence:
0
1
1
2
13
Page
9. compute GCD of two given numbers using recursion

#CODE:

def gcd(a,b):
if(b==0):
return a
else:
return gcd(b,a%b)

a=int(input("Enter first number:"))


b=int(input("Enter second
number:")) GCD=gcd(a,b)
print("GCD is:
") print(GCD)

OUTPUT:
Enter first number:5
Enter second
number:6 GCD is :
1
14
Page
10. Demonstrate exception handling

#CODE:

def divide(x,y):
return(x/y)

def calc(x,y):
return x*(divide(x,y))

try:
print(calc("hello",1))
except ZeroDivisionError:
print("you are trying to divide by zero and you
can't") except TypeError:
print("that doesn't look like a
number") except:
print("some other kind of error
occured") finally:
print("this statement executed any way"

OUTPUT:
that doesn’tlook like a number
this statement executed
anyway
15
Page
11. Demonstrate the number of vowels and consonants from string
using functions taht accept string as argument

#CODE:

def CVCF(str):
vowels = 0
consonants = 0

for i in str:
if(i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u'or i == 'A' or i == 'E' or i
== 'I' or i == 'O' or i == 'U'):
vowels = vowels +
1 else:
consonants = consonants +

1 return vowels, consonants

str = input("Please Enter Your Own String :


") vowels, consonants=CVCF(str)
print("Total Number of Vowels in this String = ", vowels)
print("Total Number of Consonants in this String = ",
consonants)

OUTPUT:
Please enter your own string :harshitha
Total number of vowels in this string=3
Total number of consonants in this
16
Page
s
t
r
i
n
g
=
6
12. copy the contents of one file into another

#CODE:

#file copy
fs=open("Source.txt","w")
fs.write("happy birth
day") fs.close()

fs=open("Source.txt","r")
fd=open("Destination.txt","w")
for i in fs:
fd.write(i)
fd.close()
fs.close()

OUTPUT:
Destination.txt happy birth
day Source.txt happy birth
day
#here the text is copied from one file to another

17
Page
13. print a list of all unique words in a file.
#CODE:

#creating empty
list l1=[]
#creating a file unique.txt and
#asking user to write data to
file #upto num lines

with open("unique.txt","w") as f:
num=int(input("enter how many
lines?")) print("Enter",num,"lines:")
for k in range(num):
data=input();
l1.append(data+"\n");
f.writelines(l1)
print(num,"lines written to
file") #creating empty set
myset=set()
#opening file unique.txt and
#reading each line,splitting that
line
#if a split is a new line then replacing with
"" #Each split words will be added to set

with open("unique.txt","r") as
f: for k in f:
sp=k.split(' ')
for k2 in sp:
myset.add(k2.replace("\n",""));
OUTPUT:
enter how many lines?
1819

3 Enter 3 lines:
PagePage
t 2 data this is line
h 3 data
i 3 lines written to file
s Unique words in the file
are:{‘line’,’is’,’1’,’data’,’2’,’this’,’3’}
i
s

l
i
n
e

d
a
t
a

t
h
i
s

i
s

l
i
n
e
14. Implement multilevel inheritance

#CODE:

class A:
def m1(self):
self.x=10
print('method 1 from class A')
class B(A):
def m2(self):
self.y=20
print('method 2 from class
B') class C(B):
def m3(self):
self.z=30
print('method 3 from class
C') print('x from A=',self.x);
print('y from B=',self.y);
print('z from C=',self.z);

obj1=C();
#Accessing methods from class A,B,C
#Accessing instance variables from class A,B,C
print("x=",obj1.x)
print("y=",obj1.y)
obj1.m1()
obj1.m2()
obj1.m3()
20

print("z=",obj1.z)
Page
print(obj1.x+obj1.y+obj1.z)

OUTPUT:

method 1 from class A


method 2 from class B
method 3 from class C
x from A=10
y from B=20
Z from
C=30 x=10
y=20
z=30
60

21
Page
15. Build GUI to convert Celcius temperature to Fahrenheit

#CODE:

from tkinter import


messagebox #Step-1 Creating
GUI Window window=Tk();
window.title("GUI Window Celsius to
Fahrenheit"); window.resizable(0,0);
window.geometry('400x100');

#Step-2 Declaring a Tk's string


variable my_var=StringVar()

#Step-3 Defining function 'compute' for


conversion def compute():
val=my_var.get()
k=int(val);
f=k*(9/5)+32
messagebox.showinfo('Msg title','Fahrenheit Value='+str(f));

#Step-4 Defining widgets Label,Entry,Button

l1=Label(window,text="Enter Celcius value:",font='100');


e1=Entry(window,width=10,font='100',textvariable=my_var)
btn=Button(window,text="Convert",fg='red',bg='light
blue',font='100',command=compute)

#Step-5 Adding widgets to Tk GUI window

l1.grid(column=1,row=0)
22

e1.grid(column=2,row=0)
btn.grid(row=1,column=1);
Page
#Step-6 Calling mainloop to enable user to close GUI
window #manually
window.mainloop();

OUTPUT:
Enter Celsius value:95
Fahrenheit
value:203.0

23
Page
MATLAB PROGRAMS

EXPERIMENT:1
Perform the arithmetic Functions of on dimensional Array
a=[12 3 4 5]; b=[6 7 8 9 10];

#CODE:

clear all;
close all;
clc;
a = [1 2 3 4 5];
b = [6 7 8 9 10];

disp('The one-dimensional array a=


');a disp('The one-dimensional array
b= ');b
% to find sum of a and b
c=a+b;
disp('The sum of a and b is ');c
% to find difference of a and
b d=a-b;
disp('The difference of a and b is ');d
%to find multiplication of a and
2(scalar) e=a*2;
disp('The product of a *2 is ');e
% to find element-by-element
multiplication f=a.*b;
disp('The element wise product of a and b is ');f
% to find element-by-element
Divison g=a./b;
disp('The element wise division of a and b is ');g
% to find the
power h = a.^2 ;
1
Page

1
disp('The power of a is ');h

2
OUTPUT:
The one-dimensional array

a= a = 1 2 3 4 5

The one-dimensional array

b= b = 6 7 8 9 10

The sum of a and b is

c=7 9 11 13 15

The difference of a and b

is d = -5 -5 -5 -5 -5

The product of a *2

is e = 2 4 6 8 10

The element wise product of a and b

is f = 6 14 24 36 50

The element wise division of a and b is

g = 0.1667 0.2857 0.3750 0.4444 0.5000

The power of a is
2
Page

h=1 4 9 16 25
EXPERIMENT-2:

Perform the statistical Functions of 1Dimensional Array a=[1 2 3 4 5]

#CODE:

clear all;
close all;
clc;
a = [1 2 3 4 5];
disp('The one-dimensional array a= ');a
% to find mean of an array
a meanOfData = mean(a) ;
disp('The mean of array a is ');meanOfData
% to find minimum value of an array a
minOfData = min(a);
disp('The minimum value of an array a is ');minOfData

% to find maximum value of an array a and its location


a [maxOfData, loc] = max(a) ;
disp('The maximum value of an array a is ');maxOfData
disp('The maximum value of an array a and its location is
');loc
% to find sum of an array
a sumOfData = sum(a);
disp('The sum of an array a is ');sumOfData

% to find standard deviation of an array


a sd = std(a);
disp('The Standard Deviation of an array a is ');sd

% to find Variance of an array a


3
Page
v = var(a);
disp('The Variance of an array a is ');v

OUTPUT:
The one-dimensional array
a= a =1 2 3 4 5

The mean of array a


is meanOfData = 3

The minimum value of an array a


is minOfData = 1

The maximum value of an array a


is maxOfData = 5

The maximum value of an array a and its location is


loc = 5

The sum of an array a


is sumOfData = 15

The Standard Deviation of an array a


is sd = 1.5811

The Variance of an array a


is v = 2.5000
4
Page
EXPERIMENT-3:

Find the addition, subtraction, multiplication, element wise multiplication and division
ofgiven matrix A=[1 2 -9: 2 -1 2:3 -4 3] B=[1 2 3 :4 5 6 :7 8 9]

#CODE:

clear all;
close all;
clc;
a=[1 2 -9 ; 2 -1 2; 3 -4 3];
b=[1 2 3; 4 5 6; 7 8 9];
disp('The matrix a= ');a
disp('The matrix b= ');b
add=a+b;
disp('The sum of a and b is
');add sub=a-b;
disp('The difference of a and b is
');sub mul=a*b;
disp('The product of a and b is
');mul elem_mul=a.*b;
disp('The element wise product of a and b is ');
elem_mul div=a./b;
disp('The element wise division of a and b is ');div

OUTPUT:

The matrix a=
5
Page
a=
1 2 -9
2 -1 2
3 -4 3

The matrix
b= b =
1 2 3
4 5 6
7 8 9

The sum of a and b


is add =
2 4 -6
6 4 8
10 4 12

The difference of a and b


is sub =
0 0 -12
-2 -6 -4
-4 -12 -6

The product of a and b is


mul =

-54 -60 -66


12 15 18
8 10 12
6
Page
The element wise product of a and b
is elem_mul =

1 4 -27
8 -5 12
21 -32 27

The element wise division of a and b is

div =

1.0000 1.0000 -3.0000


0.5000 -0.2000 0.3333
0.4286 -0.5000 0.3333

7
Page
EXPERIMRNT-4:

● Find the transpose of matrix A=[1 2 -9:2 1 -2:3 -4 3]


● Find the inverse of matrix A=[1 2 -9:2 1 -2:3 -4 3]
● Find the rank of matrix A=[1 2 -9:2 1 -2:3 -4 3]
● Find the Eigen values and vector of matrix A=[1 2 -9:2 1 -2:3 -4 3]

#CODE:

clear all;
close all;
clc;
a=[1 2 -9 ; 2 -1 2; 3 -4 3];
disp('The matrix a= ');a
% to find Transpose of matrix of
a T = (a.');
disp('The Transpose of matrix of a ');T
% Inverse of matrix of
a I=inv (a);
disp('The Inverse of matrix of a ');I
% To find Eigen values and vector of
a [eigenvector, eigenvalue] = eig(a)
disp('The Eigen values of matrix of a '); eigenvalue
disp('The Eigen vector of matrix of a ');
eigenvector

8
Page
OUTPUT:
The matrix a=a =

1 2 -9
2 -1 2
3 -4 3

The Transpose of matrix of


aT=

1 2 3
2 -1 -4
-9 2 3

The Inverse of matrix of


aI=

0.1000 0.6000 -0.1000


0 0.6000 -0.4000
-0.1000 0.2000 -0.1000

eigenvector =

0.7768 + 0i 0.7768 - 0i -0.7886 + 0i


-0.0994 - 0.3314i -0.0994 + 0.3314i -0.6113 + 0i
0.0123 - 0.5261i 0.0123 + 0.5261i -0.0661 + 0i

eigenvalue =

Diagonal Matrix
9
Page
0.6019 + 5.2417i 0 0
0 0.6019 - 5.2417i 0
0 0 1.7961 + 0i

The Eigen values of matrix of


a eigenvalue =

Diagonal Matrix

0.6019 + 5.2417i 0 0
0 0.6019 - 5.2417i 0
0 0 1.7961 + 0i

The Eigen vector of matrix of a


eigenvector =

0.7768 + 0i 0.7768 - 0i -0.7886 + 0i


-0.0994 - 0.3314i -0.0994 + 0.3314i -0.6113 + 0i
0.0123 - 0.5261i 0.0123 + 0.5261i -0.0661 + 0i

10
Page
EXPERIMENT-5:
● Find the roots of the equation 6x^5-41x^4+97x^3+41x-6

#CODE:

clear all;
close all;
clc;
v = [6, -41, 97, -97, 41,-6];
s = roots(v);
disp('The first root is: '), disp(s(1));
disp('The second root is: '), disp(s(2));
disp('The third root is: '), disp(s(3));
disp('The fourth root is: '), disp(s(4));
disp('The fifth root is: '), disp(s(5));

OUTPUT:
The first root
is: 3.0000
The second root
is: 2.0000
The third root
is: 1.0000
The fourth root is:
0.5000
The fifth root
is: 0.3333
11
Page
 Find the values of x,y,z of the equation x+y+z=3
,x+2y+3z=4,x+4y+9z=6

#CODE:

clear all;
close all;
clc;
A=[1,1,1;1,2,3;1,
4,9]; B=[3;4;6];
C=A\B
disp('Values of x,y,z of the equations is: '); C

OUTPUT:
C=
2
1
0

Values of x,y,z of the equations


is: C =
2
1
0
12
Page
● Find f(x)=8x^8-7x^7+12x^6-5x^5+8x^4+13x^3-12x+9
,compute f(2),roots of f(x) and plot for 0<=x<=20

#CODE:

clear all;close
all;clc;
p=[8 -7 12 -5 8 13 0 -12 9];
polyval(p,2);
roots(p);
x=0:0.1:20;
y=polyval(p,x);
plot(x,y)

OUTPUT:PLOT

13
Page

14
EXPERIMENT-6:
 Verification of basic properties of limits for the functionsf(x)
= (3x + 5)/(x -3) and g(x) = x2 + 1 as x ends to 4.

#CODE:

Clear all;
close all;
clc;
syms x
f = (3*x + 5)/(x-3)g
= x^2 + 1
l1 = limit(f, 4) l2
= limit (g, 4)
lAdd = limit(f + g, 4)lSub
= limit(f - g, 4)lMult =
limit(f*g, 4) lDiv =
limit (f/g, 4)

 Find the derivative of (x+2)(x^2+3) until the equation is not derivates


further

#CODE:

clear all;
14

close all;
Page
clc;
syms x ;
f=(x+2)*(x^2+3);
a=diff(f);

b=diff(a) ;
c=diff(b);
d=diff(c);

15
Page
 Calculate the area enclosed between the x-axis, and the curve y=x3-
2x+5 and the ordinates x = 1 and x = 2.

#CODE:

clear all;
close all;
clc;
syms x;
f = x^3 - 2*x +5;a
= int(f, 1, 2);
display('Area: '), disp(double(a))

16
Page
OUTPUT:

17
Page
18
Page 18
EXPERIMENT-7:
 Write the GNU Octave /Matlab code to plot vectors x =(1,2,3, 4, 5, 6)and y = (3, 1, 2, 4, 5, 1)

#CODE:
clear all;
close all;
clc;
x = [1 2 3 4 5 6];
y = [3 -1 2 4 5 1];
figure, plot(x,y)

 Write the GNU Octave /Matlab code to plot the function sin
(x) on the interval [0, 2pi] with Labels

#CODE:
clear all;
close all;
clc;
x = 0:pi/100:2*pi;y =
sin(x);
figure,plot(x,y)
xlabel('x = 0:2\pi')
ylabel('Sine of x')
title('Plot of the Sine function')
19
Page

19
OUTPUT:

20
Page
21

Page21
22
EXPERIMENT-8:
 Write the GNU Octave /Matlab code to Plot three functions ofx : y1=2cos(x), y2 = cos(x), and
y3 =0.5cos(x), in the interval 0to 2pi.

#CODE:

clear all;
close all;
clc;
x = 0:pi/100:2*pi; y1
= 2*cos(x);
y2 =
cos(x);
y3 = 0.5*cos(x);
figure,plot(x,y1,'--',x,y2,'-',x,y3,':'); xlabel('0 \leq x
\leq 2\pi'); ylabel('Cosine functions');
legend('2*cos(x)','cos(x)','0.5*cos(x)'); title('Typical
example of multiple plots');axis([0 2*pi -3 3]);

 Write the GNU Octave /Matlab code to draw multiple curves x


=0:2*pi;y1 = sin(2*x); y2 = 2*cos(2*x);

#CODE:

clear all;
close all;
23

clc;
Page

23
x = 0:pi/25:2*pi; y1

24
= sin(2*x);
y2 = 2*cos(2*x);
plot(x, y1, 'go-', 'MarkerSize', 6.0, 'MarkerEdgeColor', 'b','MarkerFaceColor', 'g');

hold on;
plot(x, y2, 'rd-', 'MarkerSize', 6.0, 'MarkerEdgeColor', 'r',
'MarkerFaceColor', 'g');
title('Plot of f(x) = sin(2x) and its
derivative');xlabel('x'); ylabel('y');
legend('f(x)', 'd/dx f(x)', 'Location', 'NorthWest');

24
Page
OUTPUT:

25
Page
26

Page27
27
28
EXPERIMENT-9:
Write the GNU Octave /Matlab code to Plot two functions using Subplots Where a=[0:5]; b = 2*a.^2
+ 3*a -5; c =1.2*a.^2+4*a-3;

#CODE:

clear all;
close all;
clc;
a = [0:0.5:5];
b = 2*a.^2 + 3*a -5;c =
1.2*a.^2+4*a-3;
subplot(1,2,1);
plot(a,b,'-or','MarkerFaceColor','g','LineWidth',2);
xlabel('X'); ylabel('Y'); legend('Curve ');subplot(1,2,2);
plot(a,c,'--ok','MarkerFaceColor','c','LineWidth',2);
xlabel('X'); ylabel('Y'); legend('Curve 2');

 Write the GNU Octave /Matlab code to Plot three functionsusing Subplots Where a = [0:5];
b = 2*a.^2 + 3*a -5; c = 1.2*a.^2+4*a-3; d = 1.2*a.^3+4*a- 3;e = 1.2*a.^4+4*a-3;

#CODE:

clear all;
close all;
clc;
29

a = [0:0.5:5];
Page

30
b = 2*a.^2 + 3*a -5;c =
1.2*a.^2+4*a-3; d =
1.2*a.^3+4*a-3; e =
1.2*a.^4+4*a-3;
subplot(2,2,1);
plot(a,b,'-or','MarkerFaceColor','g','LineWidth',2);

xlabel('X'); ylabel('Y'); legend('Curve ');subplot(2,2,2);


plot(a,c,'--ok','MarkerFaceColor','c','LineWidth',2);
xlabel('X'); ylabel('Y'); legend('Curve 2');subplot(2,2,3);
plot(a,d,'-or','MarkerFaceColor','g','LineWidth',2);
xlabel('X'); ylabel('Y'); legend('Curve 3 ');subplot(2,2,4);
plot(a,e,'--ok','MarkerFaceColor','b','LineWidth',2);
xlabel('X'); ylabel('Y'); legend('Curve 4');

30
Page
OUTPUT:

31
Page
32

Page33
33
34
EXPERIMENT-10:
 Write the GNU Octave /Matlab code to draw the surface Plot of the given function
F=(2-cos(pi*X)).*exp(Y);

#CODE:

clear
all;
close
all;
clc;
x=-1:.1:1;y=0:.1:1.5;
F=(2-
cos(pi*X)).*exp(Y);
figure,surf(X,Y,F);
xlabel('x');
ylabel('y');

 Write the GNU Octave /Matlab code to draw the mesh Plot of the given function
F=(2-cos(pi*X)).*exp(Y);

#CODE:

clear
all;
close
35

all;
Page

36
clc;
x=-1:.1:1;y=0:.1:1.5;
F=(2-
cos(pi*X)).*exp(Y);
figure,mesh(X,Y,F);
xlabel('x');
ylabel('y');

36
Page

You might also like