0% found this document useful (0 votes)
2 views3 pages

FPT Lab Programs

The document contains a series of Python programs that perform various tasks including finding the largest of three numbers, checking for palindromes, determining odd or even numbers, sorting arrays using bubble sort, and performing matrix multiplication. It also includes implementations of the Fibonacci series, bisection method, false position method, Newton-Raphson method, and Euler's method for solving equations. Each program prompts the user for input and outputs the result based on the specified functionality.

Uploaded by

akhilsrivatsa27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

FPT Lab Programs

The document contains a series of Python programs that perform various tasks including finding the largest of three numbers, checking for palindromes, determining odd or even numbers, sorting arrays using bubble sort, and performing matrix multiplication. It also includes implementations of the Fibonacci series, bisection method, false position method, Newton-Raphson method, and Euler's method for solving equations. Each program prompts the user for input and outputs the result based on the specified functionality.

Uploaded by

akhilsrivatsa27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

# 1.

Largest of 3 Numbers
a = int(input("Enter A: "))
b = int(input("Enter B: "))
c = int(input("Enter C: "))
if (a > b) and (a > c):
print("A is greater")
elif (b > a) and (b > c):
print("B is greater")
else:
print("C is greater")

# 2. Palindrome Check
num = int(input("Enter number: "))
reverse = 0
tempNum = num
while num != 0:
rem = num % 10
reverse = reverse * 10 + rem
num //= 10
if reverse == tempNum:
print("Number is a palindrome")
else:
print("Number is NOT a palindrome")

# 3. Odd or Even Check


N = int(input("Enter a number: "))
if N % 2 == 0:
print(f"{N} is Even")
else:
print(f"{N} is Odd")

# 4. Bubble Sort
arr = list(map(int, input("Enter numbers separated by space: ").split()))
N = len(arr)
for i in range(N-1):
for j in range(N-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
print("Sorted array:", arr)

# 5. Matrix Multiplication
m, n = map(int, input("Enter rows and columns of first matrix: ").split())
p, q = map(int, input("Enter rows and columns of second matrix: ").split())
if n != p:
print("Matrix multiplication not possible")
else:
a = [list(map(int, input(f"Enter row {i+1} of first matrix: ").split())) for i in range(m)]
b = [list(map(int, input(f"Enter row {i+1} of second matrix: ").split())) for i in range(p)]
c = [[0 for _ in range(q)] for _ in range(m)]
for i in range(m):
for j in range(q):
for k in range(n):
c[i][j] += a[i][k] * b[k][j]
print("Product matrix:")
for row in c:
print(*row)

# 6. Fibonacci Series up to 30 terms


a, b = 0, 1
print(a, b, end=" ")
for i in range(28):
show = a + b
print(show, end=" ")
a, b = b, show

# 7. Bisection Method
def f(x):
return x**3 - 4*x - 9

x1 = float(input("Enter x1: "))


x2 = float(input("Enter x2: "))
e = float(input("Enter error tolerance: "))
f1, f2 = f(x1), f(x2)
if f1 * f2 > 0:
print("Wrong initial guesses")
else:
while True:
x = (x1 + x2) / 2
if abs((x1 - x2) / x) < e:
break
f3 = f(x)
if f3 * f1 > 0:
x1, f1 = x, f3
else:
x2, f2 = x, f3
print("Root is:", x)

# 8. False Position Method


def f(x):
return x**3 - 4*x - 9

x0 = float(input("Enter x0: "))


x1 = float(input("Enter x1: "))
e = float(input("Enter error tolerance: "))
if f(x0) * f(x1) > 0:
print("Invalid initial guesses")
else:
while True:
x = (x0*f(x1) - x1*f(x0)) / (f(x1) - f(x0))
if abs(f(x)) < 0.00001:
break
if f(x) * f(x0) < 0:
x1 = x
else:
x0 = x
print("Root is:", x)

# 9. Newton Raphson Method


def f(x):
return x**3 - 4*x - 9

def df(x):
return 3*x**2 - 4

x = float(input("Enter initial guess: "))


e = float(input("Enter error tolerance: "))
n = int(input("Enter max iterations: "))
d = float(input("Enter minimum slope check value: "))
for i in range(1, n+1):
fx, f1x = f(x), df(x)
if abs(f1x) < d:
print("Too small slope")
break
x1 = x - fx/f1x
if abs((x1-x)/x1) < e:
print("Root is:", x1)
break
x = x1
else:
print("Does not converge")

# 10. Euler's Method


def f(x, y):
return x + y

x0 = float(input("Enter x0: "))


y0 = float(input("Enter y0: "))
h = float(input("Enter h: "))
xn = float(input("Enter xn: "))
n = int((xn-x0)/h)+1
for i in range(1, n+1):
y = y0 + h * f(x0, y0)
x = x0 + h
print(f"At x={x:.2f}, y={y:.4f}")
x0, y0 = x, y

You might also like