0% found this document useful (0 votes)
25 views18 pages

Manish PDF

The document contains a series of Python programs demonstrating various mathematical and computational concepts, including arithmetic operations, Fibonacci sequence calculation, string manipulation, list operations, matrix operations, and function usage. Each program includes user input, processing, and output examples. Additionally, it features a submission section for a student named Manish Kumar from Aggarwal College for the course Mathematical Computing using Python.

Uploaded by

ruqayyah240711
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)
25 views18 pages

Manish PDF

The document contains a series of Python programs demonstrating various mathematical and computational concepts, including arithmetic operations, Fibonacci sequence calculation, string manipulation, list operations, matrix operations, and function usage. Each program includes user input, processing, and output examples. Additionally, it features a submission section for a student named Manish Kumar from Aggarwal College for the course Mathematical Computing using Python.

Uploaded by

ruqayyah240711
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

Program 1: Arithmetic Operations

a = int(input("Enter first number: "))


b = int(input("Enter second number: "))

print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Floor Division:", a // b)
print("Modulus:", a % b)
print("Exponentiation:", a ** b)

Output:

Enter first number: 10


Enter second number: 3
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.3333333333333335
Floor Division: 3
Modulus: 1
Exponentiation: 1000
Program 2: Nth Fibonacci Number
def fibonacci(n):
if n <= 0:
return "Invalid input"
elif n == 1:
return 0
elif n == 2:
return 1
a, b = 0, 1
for _ in range(2, n):
a, b = b, a + b
return b

n = int(input("Enter position n: "))


print(f"Fibonacci number at position {n}:", fibonacci(n))

Output: Enter position n: 7

Fibonacci number at position 7: 8


Program 3: Sum of Digits
n = int(input("Enter a number: "))
total = 0
temp = abs(n)
while temp > 0:
total += temp % 10
temp //= 10
print("Sum of digits:", total)

Output:

Enter a number: 1234


Sum of digits: 10
Program 4: Different Patterns
n = int(input("Enter number of rows: "))

print("--- Right Triangle ---")


for i in range(1, n+1):
print("* " * i)

print("--- Inverted Triangle ---")


for i in range(n, 0, -1):
print("* " * i)

print("--- Number Pattern ---")


for i in range(1, n+1):
print(*range(1, i+1))

print("--- Pyramid ---")


for i in range(1, n+1):
print(" " * (n-i) + "* " * i)
Output:

Enter number of rows: 4


--- Right Triangle ---
*
* *
* * *
* * * *
--- Inverted Triangle ---
* * * *
* * *
* *
*
--- Number Pattern ---
1
1 2
1 2 3
1 2 3 4
--- Pyramid ---
*
* *
* * *
* * * *
Program 5: Sum of Elements in List
lst = list(map(int, input("Enter elements separated by space:
").split()))
print("List:", lst)
print("Sum of elements:", sum(lst))

# Manual method
total = 0
for x in lst:
total += x
print("Sum (manual loop):", total)

Output:

Enter elements separated by space: 10 20 30 40 50


List: [10, 20, 30, 40, 50]
Sum of elements: 150
Sum (manual loop): 150
Program 6: Compare Two Lists
lst1 = list(map(int, input("Enter elements of list 1: ").split()))
lst2 = list(map(int, input("Enter elements of list 2: ").split()))

print("List 1:", lst1)


print("List 2:", lst2)

if lst1 == lst2:
print("Both lists are equal.")
else:
print("Lists are not equal.")

print("Common elements:", list(set(lst1) & set(lst2)))


print("Elements only in list 1:", list(set(lst1) - set(lst2)))
print("Elements only in list 2:", list(set(lst2) - set(lst1)))

Output:

Enter elements of list 1: 1 2 3 4 5


Enter elements of list 2: 3 4 5 6 7
List 1: [1, 2, 3, 4, 5]
List 2: [3, 4, 5, 6, 7]
Lists are not equal.
Common elements: [3, 4, 5]
Elements only in list 1: [1, 2]
Elements only in list 2: [6, 7]
Program 7: Palindrome Check
s = input("Enter a string: ")
clean = [Link](" ", "").lower()
if clean == clean[::-1]:
print(f'"{s}" is a Palindrome')
else:
print(f'"{s}" is NOT a Palindrome')

Output:
Enter a string: racecar
"racecar" is a Palindrome

Enter a string: hello


"hello" is NOT a Palindrome
Program 8: Reverse a String

s = input("Enter a string: ")

# Method 1: Slicing
print("Reversed (slicing):", s[::-1])

# Method 2: Loop
rev = ""
for ch in s:
rev = ch + rev
print("Reversed (loop):", rev)

# Method 3: reversed()
print("Reversed (built-in):", "".join(reversed(s)))

Output:

Enter a string: Python


Reversed (slicing): nohtyP
Reversed (loop): nohtyP
Reversed (built-in): nohtyP
Program 9: String Concatenation,
Slicing & Indexing
s1 = input("Enter first string: ")
s2 = input("Enter second string: ")

# Concatenation
print("Concatenation:", s1 + s2)
print("Repetition:", s1 * 2)

# Indexing
print("First char:", s1[0])
print("Last char:", s1[-1])

# Slicing
print("First 3 chars:", s1[:3])
print("Last 3 chars:", s1[-3:])
print("Every 2nd char:", s1[::2])
print("Reversed:", s1[::-1])

Output:

Enter first string: Hello


Enter second string: World
Concatenation: HelloWorld
Repetition: HelloHello
First char: H
Last char: o
First 3 chars: Hel
Last 3 chars: llo
Every 2nd char: Hlo
Reversed: olleH
Program 10: Simple Calculator using
Functions

def add(a, b): return a + b


def subtract(a, b): return a - b
def multiply(a, b): return a * b
def divide(a, b):
if b == 0:
return "Error: Division by zero"
return a / b

a = float(input("Enter first number: "))


op = input("Enter operator (+, -, *, /): ")
b = float(input("Enter second number: "))

if op == "+": print("Result:", add(a, b))


elif op == "-": print("Result:", subtract(a, b))
elif op == "*": print("Result:", multiply(a, b))
elif op == "/": print("Result:", divide(a, b))
else: print("Invalid operator")

Output:

Enter first number: 15


Enter operator (+, -, *, /): /
Enter second number: 4
Result: 3.75
Program 11: Demonstrate Use of
Functions
def greet(name):
print(f"Hello, {name}!")

def square(n):
return n ** 2

def is_even(n):
return n % 2 == 0

def factorial(n):
if n == 0 or n == 1:
return 1
return n * factorial(n - 1)

def max_of_three(a, b, c):


return max(a, b, c)

greet("Alice")
print("Square of 6:", square(6))
print("Is 8 even?", is_even(8))
print("Factorial of 5:", factorial(5))
print("Max of 3, 9, 5:", max_of_three(3, 9, 5))

Output:

Hello, Alice!
Square of 6: 36
Is 8 even? True
Factorial of 5: 120
Max of 3, 9, 5: 9
Program 12: Default Parameters
def greet(name, msg="Good Morning"):
print(f"{msg}, {name}!")

def power(base, exp=2):


return base ** exp

def student_info(name, age=18, course="BCA"):


print(f"Name: {name}, Age: {age}, Course: {course}")

greet("Bob")
greet("Alice", "Good Evening")
print(power(3))
print(power(2, 8))
student_info("Gagan")
student_info("Riya", 20, "MCA")

Output:

Good Morning, Bob!


Good Evening, Alice!
9
256
Name: Gagan, Age: 18, Course: BCA
Name: Riya, Age: 20, Course: MCA
Program 13: Matrix Multiplication
(DotProduct)

def mat_mul(A, B):


rows_A, cols_A = len(A), len(A[0])
cols_B = len(B[0])
result = [[0]*cols_B for _ in range(rows_A)]
for i in range(rows_A):
for j in range(cols_B):
for k in range(cols_A):
result[i][j] += A[i][k] * B[k][j]
return result

A = [[1,2],[3,4]]
B = [[5,6],[7,8]]

print("Matrix A:")
for row in A: print(row)
print("Matrix B:")
for row in B: print(row)
print("Result (A x B):")
for row in mat_mul(A, B): print(row)

Output:

Matrix A:
[1, 2]
[3, 4]
Matrix B:
[5, 6]
[7, 8]
Result (A x B):
[19, 22]
[43, 50]
Program 14: Inverse of a Matrix
import numpy as np

A = [Link]([[1,2],[3,4]])
print("Matrix A:")
print(A)

det = [Link](A)
print("Determinant:", round(det, 2))

if det != 0:
inv = [Link](A)
print("Inverse of A:")
print(inv)
print("Verification A * A_inv:")
print([Link](A @ inv))
else:
print("Matrix is singular. Inverse doesn't exist.")

Output:

Matrix A:
[[1 2]
[3 4]]
Determinant: -2.0
Inverse of A:
[[-2. 1. ]
[ 1.5 -0.5]]
Verification A * A_inv:
[[1. 0.] [0. 1.]
Program 15: Symmetric Matrix Check

import numpy as np

def is_symmetric(matrix):
arr = [Link](matrix)Program 15: Symmetric Matrix Check
python# Program 15: Check if Matrix is Symmetric
import numpy as np

def is_symmetric(matrix):
arr = [Link](matrix)
return np.array_equal(arr, arr.T)

# Symmetric matrix
A = [[1,2,3],[2,5,6],[3,6,9]]
print("Matrix A:")
for row in A: print(row)
print("Transpose:")
for row in [Link](A).[Link](): print(row)
print("Is Symmetric?", is_symmetric(A))

# Non-symmetric matrix
B = [[1,2],[3,4]]
print("\nMatrix B:")
for row in B: print(row)
print("Is Symmetric?", is_symmetric(B))
return np.array_equal(arr, arr.T)

# Symmetric matrix
A = [[1,2,3],[2,5,6],[3,6,9]]
print("Matrix A:")
for row in A: print(row)
print("Transpose:")
for row in [Link](A).[Link](): print(row)
print("Is Symmetric?", is_symmetric(A))
# Non-symmetric matrix
B = [[1,2],[3,4]]
print("\nMatrix B:")
for row in B: print(row)
print("Is Symmetric?", is_symmetric(B))

Output:

Matrix A:
[1, 2, 3]
[2, 5, 6]
[3, 6, 9]
Transpose:
[1, 2, 3]
[2, 5, 6]
[3, 6, 9]
Is Symmetric? True

Matrix B:
[1, 2]
[3, 4]
Is Symmetric? False
AGGARWAL COLLEGE

SESSION:2025-2026

SUBJECT: MATHEMATICAL
COMPUTING USING PYTHON

SUBMITTED BY – MANISH KUMAR

NAME: MANISH KUMAR


ROLLNO: 109 (SEC - A)
COURSE: BCA ,2ND YR
SESSION: 2025 -2026
SUBJECT : MATHEMATICAL COMPUTING
USING PYTHON

You might also like