Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Program 1

Python program to find roots of quadratic equation

Text code:
import math

# function for finding roots

def quadroot( a, b, c):

dis = b * b - 4 * a * c

sqrt_val = math.sqrt(abs(dis))

if dis > 0:

print("real and different roots")

print((-b + sqrt_val)/(2 * a))

print((-b - sqrt_val)/(2 * a))

elif dis == 0:

print("real and same roots")

print(-b / (2 * a))
Program 2
To find and delete repeating elements in given list.

Text code:

def remove_duplicate(input_list):

new_list=[]

for num in input_list:

if num not in new_list:

new_list.append(num)

return new_list

input_list=[5,2,4,6,8,9,2,4,45,56,7,8,12,45678,56,89,14]

print ("The given list is :",input_list)

print ("List after removing duplicate elements : ", remove_duplicate(input_list)

Output

The given list is : [5, 2, 4, 6, 8, 9, 2, 4, 45, 56, 7, 8, 12, 45678, 56, 89, 14]

List after removing duplicate elements : [5, 2, 4, 6, 8, 9, 45, 56, 7, 12, 45678, 89, 14]
Program 3
Write a program to input ant print the element sum of user defined
matrix.

Text code:

r=int(input("Enter the no of rows in matrix :"))

c=int(input("Enter the no of columns in matrix:"))

sum=0

matrix=[]

print ("Enter the values in row wise manner:")

for i in range(r):

a=[]

for j in range(c):

a.append(int(input()))

matrix.append(a)

#Printing and finding sum of matrix

print("Printing the matrix:")

for i in range(r):

for j in range(c):

print(matrix[i][j], end = " ")

sum=sum+matrix[i][j]

print()
Program 4
Write a program to input and multiply two matrices.

Text code:

def printmatrix(M,r,c):

#Printing the matrix

for i in range(r):

for j in range(c):

print(M[i][j], end = " ")

print()

def matrixmulti(r1,c1,A,r2,c2,B):

#Multiplying the matrix

C=[[0 for j in range(c2)] for i in range(r1)]

if (r2!=c1):

print("Matrix multiplication is not possible")

return

for i in range(r1):

for j in range(c2):

for k in range(c1):

C[i][j]=C[i][j]+A[i][k]*B[k][j]

print("The product of matrix A and B is:")


Program 5
Write a program to find eigenvalue and eigenvectors of given 3*3 matrix
using Numpy.

Text code:
import numpy as np

matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

eigenvalues, eigenvectors = np.linalg.eig(matrix)

print("The eigenvalues of the matrix are:")

print(eigenvalues)

print("The eigenvectors of the matrix are:")

print(eigenvectors)

Output

The eigenvalues of the matrix are:

[ 1.61168440e+01 -1.11684397e+00 -1.30367773e-15]

The eigenvectors of the matrix are:

[[-0.23197069 -0.78583024 0.40824829]

[-0.52532209 -0.08675134 -0.81649658]


Program 6
Write a program to find a solution of linear equation in (y = mx +
c) .

Text code:
def solve_linear_equation(m, c, x):

y=m*x+c

return y

# Get the input values

m = float(input("Enter the slope (m): "))

c = float(input("Enter the y-intercept (c): "))

x = float(input("Enter the x-value: "))

# Solve for y

y = solve_linear_equation(m, c, x)

# Print the solution

print("The solution to the equation y = mx + c is y = " ,y)

Output
Enter the slope (m): 3

Enter the y-intercept (c): 1

Enter the x-value: 2


Program 7
Write a Program to plot a line given using euation y = mx + c.

Text code:
import numpy as np

import matplotlib.pyplot as plt

# Given slope (m) and y-intercept (c)

m=2

c=1

# Construct an array of x-values

x = np.linspace(0, 10, 100) # Creates a numpy array of [0.0, 0.1, ..., 10.0]

# Calculate corresponding y-values

y=m*x+c

# Plot the line

plt.plot(x, y, linestyle='solid', label='y = {}x + {}'.format(m, c))

plt.xlabel('x')

plt.ylabel('y')

plt.title('Linear Equation: y = mx + c')

plt.grid(True)

plt.legend()

plt.show()

You might also like