Professional Documents
Culture Documents
D
COMPUTER
SKILLS(ACS)
RECORD
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
#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)
#tuple
list(mytuple) mynewlist[1] =
Page
"kiwi" mytuple =
tuple(mynewlist)
print(mytuple)
#set
myset = {"apple", "banana",
"cherry"} 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:"))
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)
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 +
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:
21
Page
15. Build GUI to convert Celcius temperature to Fahrenheit
#CODE:
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];
1
disp('The power of a is ');h
2
OUTPUT:
The one-dimensional array
a= a = 1 2 3 4 5
b= b = 6 7 8 9 10
c=7 9 11 13 15
is d = -5 -5 -5 -5 -5
The product of a *2
is e = 2 4 6 8 10
is f = 6 14 24 36 50
The power of a is
2
Page
h=1 4 9 16 25
EXPERIMENT-2:
#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
OUTPUT:
The one-dimensional array
a= a =1 2 3 4 5
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
1 4 -27
8 -5 12
21 -32 27
div =
7
Page
EXPERIMRNT-4:
#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
1 2 3
2 -1 -4
-9 2 3
eigenvector =
eigenvalue =
Diagonal Matrix
9
Page
0.6019 + 5.2417i 0 0
0 0.6019 - 5.2417i 0
0 0 1.7961 + 0i
Diagonal Matrix
0.6019 + 5.2417i 0 0
0 0.6019 - 5.2417i 0
0 0 1.7961 + 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
#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)
#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]);
#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);
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