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

Python Programmes Lab

The document outlines various mathematical computations including finding squares, cubes, square roots, factorials, logarithms, roots of quadratic equations, trigonometric values, point classification, sum of digits, prime number checks, palindrome checks, least squares method, and the bisection method. Each section includes an aim, algorithm, program code, results, and viva questions for deeper understanding. The programs utilize Python's math module and demonstrate essential mathematical concepts and their implementations.

Uploaded by

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

Python Programmes Lab

The document outlines various mathematical computations including finding squares, cubes, square roots, factorials, logarithms, roots of quadratic equations, trigonometric values, point classification, sum of digits, prime number checks, palindrome checks, least squares method, and the bisection method. Each section includes an aim, algorithm, program code, results, and viva questions for deeper understanding. The programs utilize Python's math module and demonstrate essential mathematical concepts and their implementations.

Uploaded by

gotkhararyan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Square, Cube, Square Root & Factorial

Aim

To find square, cube, square root and factorial of a number.

Algorithm

1. Start
2. Read a number n
3. Compute square = n²
4. Compute cube = n³
5. Compute square root using sqrt()
6. Compute factorial using factorial()
7. Display results
8. Stop

Program
import math
n = int(input("Enter a number: "))
print("Square:", n**2)
print("Cube:", n**3)
print("Square Root:", [Link](n))
print("Factorial:", [Link](n))

Sample Output
Enter a number: 5
Square: 25
Cube: 125
Square Root: 2.236
Factorial: 120

Result

Thus, the square, cube, square root and factorial of the given number were calculated
successfully.

Viva Questions

1. What is factorial?
Product of all positive integers up to n.
2. What is sqrt function?
It returns the square root of a number.
3. Which module is used?
math module

Logarithm and Anti-logarithm


Aim

To find logarithm and anti-logarithm of a number.

Algorithm

1. Start
2. Input number n
3. Find log10(n) and ln(n)
4. Find anti-log = 10ⁿ
5. Display results
6. Stop

Program
import math
n = float(input("Enter a number: "))
print("Log10:", math.log10(n))
print("Natural Log:", [Link](n))
print("Anti-log:", 10**n)

Result
Thus, logarithm and anti-logarithm of the given number were obtained.
Viva Questions
1. Difference between log and ln?
log → base 10, ln → base e.
2. What is anti-log?
Inverse of logarithm (10^x).

Roots of Quadratic Equation


Aim

To find roots of a quadratic equation.

Algorithm

1. Input a, b, c
2. Compute discriminant D = b² - 4ac
3. If D > 0 → real & distinct
4. If D = 0 → equal roots
5. If D < 0 → complex roots
6. Display roots

Program
import math
a = float(input("Enter a: "))
b = float(input("Enter b: "))
c = float(input("Enter c: "))

d = b**2 - 4*a*c

if d > 0:
r1 = (-b + [Link](d)) / (2*a)
r2 = (-b - [Link](d)) / (2*a)
print("Roots:", r1, r2)
elif d == 0:
print("Root:", -b/(2*a))
else:
print("Complex roots")

Result

Thus, roots of the quadratic equation were determined successfully.

Viva Questions

1. What is discriminant?
D=b2−4acD = b^2 - 4acD=b2−4ac
2. When are roots real?
When D ≥ 0.
3. When are roots complex?
When D < 0.

Trigonometric Functions
Aim

To find trigonometric values.

Algorithm

1. Input angle in degrees


2. Convert to radians
3. Compute sin, cos, tan
4. Compute cosec, sec, cot
5. Display results

Program
import math
x = float(input("Enter angle: "))
r = [Link](x)

print("sin:", [Link](r))
print("cos:", [Link](r))
print("tan:", [Link](r))
print("cosec:", 1/[Link](r))
print("sec:", 1/[Link](r))
print("cot:", 1/[Link](r))

Result
Thus, trigonometric values of the given angle were calculated.
Viva Questions
1. Why convert degrees to radians?
Python trig functions use radians.
2. Relation between sin and cosec?
cosec = 1/sin.

Classification of Point
Aim

To classify a point based on coordinates.

Program
x = float(input("Enter x: "))
y = float(input("Enter y: "))

if x == 0 and y == 0:
print("Origin")
elif x == 0:
print("Y-axis")
elif y == 0:
print("X-axis")
elif x > 0 and y > 0:
print("1st Quadrant")
elif x < 0 and y > 0:
print("2nd Quadrant")
elif x < 0 and y < 0:
print("3rd Quadrant")
else:
print("4th Quadrant")
Result
Thus, the given point was classified correctly.
Viva Questions
1. What is origin?
(0,0)
2. Quadrants?
4 regions in coordinate plane.

Sum of Digits
Aim

To find sum of digits of a number.

Program
n = int(input("Enter number: "))
s = 0

while n > 0:
s += n % 10
n //= 10

print("Sum:", s)

Result
Thus, sum of digits of the number was found.
Viva Questions
1. What does % operator do?
Gives remainder.
2. Why use //?
Removes last digit.

Prime Number Check


Aim

To check whether a number is prime.

Program
n = int(input("Enter number: "))

if n > 1:
for i in range(2, int(n**0.5)+1):
if n % i == 0:
print("Not Prime")
break
else:
print("Prime")
else:
print("Not Prime")

Result
Thus, the given number was checked for primality.
Viva Questions
1. What is prime number?
Number with only two factors.
2. Why check till √n?
Efficient method to reduce iterations.

Prime Numbers up to N
Aim

To generate prime numbers up to a limit.

Program
n = int(input("Enter limit: "))

for num in range(2, n+1):


for i in range(2, int(num**0.5)+1):
if num % i == 0:
break
else:
print(num, end=" ")

Result
Thus, all prime numbers up to the given limit were generated.
Viva Questions
1. What is range function?
Generates sequence of numbers.
2. Time complexity?
Approximately O(n√n).
Palindrome
Aim

To check whether a number is palindrome.

Program
n = input("Enter number: ")

if n == n[::-1]:
print("Palindrome")
else:
print("Not Palindrome")

Result

Thus, the given number was checked whether it is palindrome or not.

Viva Questions

1. What is palindrome?
Same forward and backward.
2. What is slicing?
Used to reverse string ([::-1]).

Least Squares Method


Aim

To fit a straight line using least squares method.

Program
n = int(input("Enter number of points: "))
x, y = [], []

for i in range(n):
[Link](float(input("x: ")))
[Link](float(input("y: ")))

sum_x = sum(x)
sum_y = sum(y)
sum_xy = sum(x[i]*y[i] for i in range(n))
sum_x2 = sum(i**2 for i in x)
m = (n*sum_xy - sum_x*sum_y) / (n*sum_x2 - sum_x**2)
c = (sum_y - m*sum_x) / n

print("y =", m, "x +", c)

Result

Thus, the best fit straight line was obtained using least squares method.

Viva Questions

1. What is least squares?


Minimizes error between observed and predicted values.
2. Equation form?
y = mx + c

Bisection Method
Aim

To find root using bisection method.

Program
def f(x):
return x**3 - x - 2

a = float(input("Enter a: "))
b = float(input("Enter b: "))
tol = float(input("Tolerance: "))

if f(a)*f(b) >= 0:
print("Invalid interval")
else:
while (b - a)/2 > tol:
c = (a + b)/2
if f(c) == 0:
break
elif f(a)*f(c) < 0:
b = c
else:
a = c

print("Root:", (a+b)/2)
Result

Thus, the root of the equation was found using bisection method.

Viva Questions

1. What is bisection method?


Numerical method to find roots.
2. Condition for applying?
f(a) × f(b) < 0
3. Advantage?
Simple and reliable.

You might also like