Sunday, October 22, 2023

Python Programming - Lab- Core Lab 5 - Bsc CS DA -Bharathiar University - Program 8 - Write recursive functions to display prime number from 2 to n.

 

 B.Sc Computer Science with Data Analytics  

Python Programming - Lab

Core Lab 5  

Bharathiar University 

Program 8 - Write recursive functions to display prime number from 2 to n.


SOURCE CODE:

def is_prime(num, divisor=2):
if num <= 2:
return num == 2
if num % divisor == 0:
return False
if divisor * divisor > num:
return True
return is_prime(num, divisor + 1)

def display_primes(n, current=2):
if current <= n:
if is_prime(current):
print(current, end=" ")
display_primes(n, current + 1)

# read n value from user
num = int(input("Enter a number (n) to display prime numbers from 2 to n: "))

# Check if the input is non-negative
if num < 2:
print("Prime numbers start from 2. Please enter a number greater than or equal to 2.")
else:
print("Prime numbers from 2 to", num, "are:")
display_primes(num)

OUTPUT

Enter a number (n) to display prime numbers from 2 to n: 10
Prime numbers from 2 to 10 are:
2 3 5 7 

Saturday, October 21, 2023

Python Programming - Lab- Core Lab 5 - Bsc CS DA -Bharathiar University - Program 7 -Write recursive functions for Fibonacci Sequence up to given number n.

 

 B.Sc Computer Science with Data Analytics  

Python Programming - Lab

Core Lab 5  

Bharathiar University 

Program 7 -  Write recursive functions for Fibonacci Sequence up to given number n.


SOURCE CODE:

def fibonacci(n, a=0, b=1):
if n == 0:
return []
elif n == 1:
return [a]
else:
if a + b > n:
return [a]
else:
return [a] + fibonacci(n, b, a + b)

# read a number
num = int(input("Enter a number to generate Fibonacci sequence up to that number: "))

# Check if the input is non-negative
if num < 0:
print("Please enter a non-negative number.")
else:
fib_sequence = fibonacci(num)
print("Fibonacci sequence up to", num, "is:", fib_sequence)

OUTPUT:

Enter a number to generate Fibonacci sequence up to that number: 5
Fibonacci sequence up to 5 is: [0, 1, 1, 2, 3]

Python Programming - Lab- Core Lab 5 - Bsc CS DA -Bharathiar University - Program 6 - Write recursive functions for the factorial of positive integer.

 

 B.Sc Computer Science with Data Analytics  

Python Programming - Lab

Core Lab 5  

Bharathiar University 

Program 6 - Write recursive functions for the factorial of positive integer.


SOURCE CODE:


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

# Read an integer
num = int(input("Enter a positive integer: "))

# Check if the input is a positive integer
if num < 0:
print("Factorial is not defined for negative numbers.")
else:
result = factorial(num)
print("Factorial of", num, "is:", result)

OUTPUT:

Enter a positive integer: 5
Factorial of 5 is: 120

Friday, October 20, 2023

Python Programming - Lab- Core Lab 5 - Bsc CS DA -Bharathiar University - Program 5 - Write recursive functions for GCD of two integers.

 

 B.Sc Computer Science with Data Analytics  

Python Programming - Lab

Core Lab 5  

Bharathiar University 

Program 5 - Write recursive functions for GCD of two integers.


SOURCE CODE

def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)

# Example usage
num1 = int(input("Enter the first integer: "))
num2 = int(input("Enter the second integer: "))

result = gcd(num1, num2)
print("GCD of", num1, "and", num2, "is:", result)

OUTPUT:
Enter the first integer: 12
Enter the second integer: 24
GCD of 12 and 24 is: 12

Python Programming - Lab- Core Lab 5 - Bsc CS DA -Bharathiar University - Program 4 - Write a python program to find the product of two matrices [A]mxp and [B]pxr


Python Programming - Lab

Core Lab 5  

Bharathiar University 

Program 4 - Write a python program to find the product of two matrices [A]mxp and [B]pxr

Source Code

# Function to input a matrix from the user
def input_matrix(rows, cols):
matrix = []
print("Enter the matrix of size {",rows,"}x{",cols,"}:")
for i in range(rows):
r = list(map(int, input().split()))
if len(r) != cols:
print("Invalid input. Please enter exactly", cols, "numbers.")
return None
matrix.append(r)
return matrix

# Function to multiply two matrices
def multiply_matrices(A,B):
# Get dimensions of matrices A and B
m, p = len(A), len(A[0])
p, r = len(B), len(B[0])

# Check if matrices can be multiplied
if p != len(A[0]):
return "Matrices cannot be multiplied. Invalid dimensions."

# Initialize the result matrix with zeros
result = [[0 for _ in range(r)] for _ in range(m)]

# Perform matrix multiplication
for i in range(m):
for j in range(r):
for k in range(p):
result[i][j] += A[i][k] * B[k][j]

return result


# Get dimensions of matrices from the user
m = int(input("Enter the number of rows for matrix A: "))
p = int(input("Enter the number of columns for matrix A and rows for matrix B: "))
r = int(input("Enter the number of columns for matrix B: "))

# Input matrices from the user
matrix_A = input_matrix(m, p)
matrix_B = input_matrix(p, r)

# Check if input matrices are valid
if matrix_A and matrix_B:
# Calculate the product of matrices A and B
result_matrix = multiply_matrices(matrix_A, matrix_B)

# Display the result
if isinstance(result_matrix, str):
print(result_matrix) # If matrices cannot be multiplied
else:
print("Product of matrices A and B:")
for row in result_matrix:
print(row)


OUTPUT

Enter the number of rows for matrix A: 2
Enter the number of columns for matrix A and rows for matrix B: 2
Enter the number of columns for matrix B: 2
Enter the matrix of size { 2 }x{ 2 }:
1 2
1 2
Enter the matrix of size { 2 }x{ 2 }:
2 4
2 4
Product of matrices A and B:
[6, 12]
[6, 12]

Program 12 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 12 Implement a telephone directory using files

  BCA Object Oriented Programming using C++ Practical Madras University  Program 12  Implement a telephone directory using files SOURCE CODE...