0% found this document useful (0 votes)
5 views

python lab

The document contains Python code for solving various mathematical problems, including finding roots of quadratic equations, generating Pascal's triangle, performing operations on complex numbers, and manipulating 3D vectors. It also includes a function to convert integers to Roman numerals. Each section of the code prompts the user for input and displays the results of the calculations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

python lab

The document contains Python code for solving various mathematical problems, including finding roots of quadratic equations, generating Pascal's triangle, performing operations on complex numbers, and manipulating 3D vectors. It also includes a function to convert integers to Roman numerals. Each section of the code prompts the user for input and displays the results of the calculations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#question no.

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)

print("Root 1:", root1)


print("Root 2:", root2)

a=int(input("enter the coefficent of x^2:"))


b=int(input("enter the coefficent of x:"))
c=int(input("enter the constant terms:"))
_quadratic(a,b,c)

#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)

def __truediv__(self, other):


denom = other.real ** 2 + other.imag ** 2
if denom == 0:
raise ZeroDivisionError("Cannot divide by zero-valued complex number.")
real = (self.real * other.real + self.imag * other.imag) / denom
imag = (self.imag * other.real - self.real * other.imag) / denom
return complex(real, imag)
x1=int(input("enter the first real part:"))
x2=int(input("enter the first imaginary parts:"))
y1=int(input("enter the second real parts:"))
y2=int(input("enter the second imaginary parts :"))
c1=complex(x1,y1)
c2=complex(x2,y2)
c1.display()
c2.display()
a=c1 + c2
print("the additon result:",end="")
a.display()
b=c1 - c2
print("the subtraction result:",end="")
b.display()
d=c1 *c2
print("the multiplication result:",end="")
d.display()
e=c1/c2
print("the division result:",end="")
e.display()

#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)}")

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy