python lab
python lab
1
import math
import cmath
def _quadratic(a,b,c):
D=b*b-4*a*c
if D>0:
print("The equation has real and unequal roots.")
root1=(-b +math.sqrt(D)) /(2*a)
root2=(-b -math.sqrt(D)) /(2*a)
print("Root 1:", root1)
print("Root 2:", root2)
if D==0:
print("The equation has real and equal roots.")
root1=(-b) /(2*a)
root2=(-b) /(2*a)
print("Root 1:", root1)
print("Root 2:", root2)
if D<0:
print("The equation has imaginary roots.")
root1 = (-b + cmath.sqrt(D)) / (2*a)
root2 = (-b - cmath.sqrt(D)) / (2*a)
#question no.2
import math
def _pascal_tri(n):
for i in range(n):
print(' ' * (n - i), end='') # for alignment
for j in range(i + 1):
val = math.comb(i, j) # Binomial coefficient nCr
print(val, end=' ')
print()
n=int(input("number of rows:"))
_pascal_tri(n)
#question number .3
import cmath
class Quadratic:
def _init_(self,a,b,c):
self.a=a
self.b=b
self.c=c
def root(self):
D=self.b**2-4*self.a*self.c
root1=(-self.b+cmath.sqrt(D))/(2*self.a)
root2=(-self.b-cmath.sqrt(D))/(2*self.a)
return root1 ,root2
a=float(input("enter the coeffient of x^2:"))
b=float(input("enter the coefficent of x:"))
c=float(input("enter the constant terms:"))
quad_eqa = Quadratic(a , b , c)
r1,r2 =quad_eqa.root()
print(f"the equation of roots{r1}and{r2}" )
#question no.4
class complex:
def __init__(self,real,imag):
self.real=real
self.imag=imag
def display(self):
if self.imag>=0:
sign="+"
else:
sign='-'
print(f"the complex number are :{self.real}{sign}{abs(self.imag)}j" )
def __add__(self,other):
real_sum=self.real+other.real
imag_sum=self.imag+other.imag
return complex(real_sum, imag_sum)
def __sub__(self,other):
real_sub=self.real-other.real
imag_sub=self.imag-other.imag
return complex(real_sub, imag_sub)
def __mul__(self, other):
real = self.real * other.real - self.imag * other.imag
imag = self.real * other.imag + self.imag * other.real
return complex(real, imag)
#question number 5
#question no. 6
import math
class vec3d:
def __init__(self,x,y,z):
self.x=x
self.y=y
self.z=z
def __str__(self):
return(f"{self.x}i+{self.y}j+{self.z}k")
def add(self,b):
return (f"{self.x+b.x}i+{self.y+b.y}j+{self.z+b.z}k")
def sub(self,b):
return (f"{self.x-b.x}i+{self.y-b.y}j+{self.z-b.z}k")
def dot(self,b):
return (f"{self.x*b.x+self.y*b.y+self.z*b.z}")
def cross(self,b):
return(f"{self.y*b.z-self.z*b.y}i+{b.x*self.z-self.x*b.z}j+{self.x*b.y-
self.y*b.x}k")
def len(self):
a=(self.x**2+self.y**2+self.z**2)**.5
return a
def norm(self):
length = self.len()
if length == 0:
raise ValueError("Cannot normalize a zero-length vector.")
return vec3d(self.x / length, self.y / length, self.z / length)
x1=int(input("enter the vector in x axis:"))
y1=int(input("enter the vector in y axis:"))
z1=int(input("enter the vectors in z xis:"))
x2=int(input("enter the vector in x axis:"))
y2=int(input("enter the vector in y axis:"))
z2=int(input("enter the vectors in z xis:"))
v1=vec3d(x1,y1,z1)
v2=vec3d(x2,y2,z2)
print(f"the first vector:{v1}")
print(f"the second vector:{v2}")
print(f"the addition result:{v1.add(v2)}")
print(f"the subtraction result:{v1.sub(v2)}")
print(f"the dot product result:{v1.dot(v2)}")
print(f"the cross product result:{v1.cross(v2)}")
print(f"the length of vector result{v1}:{v1.len():.2f}")
print(f"the length of vector result{v2}:{v2.len():.2f}")
print(f"the normalized vector {v1}:{v1.norm()}")
print(f"the normalized vector {v2}:{v2.norm()}")
#question 7
def roman(num):
r=[("M",1000),("CM",900),("D",500),("CD",400),("C",100),("XC",90),("L",50),
("XL",40),("X",10),("IX",9),("V",5),("IV",4),("I",1)]
result=""
for symbol, value in r:
while num >= value:
result+=symbol
num-=value
return result
num1=1357
num2=963
print(f"the value of {num1} in roman:{roman(num1)}")
print(f"the value of {num2} in roman:{roman(num2)}")