Generated 14 Programs
Generated 14 Programs
if number % 2 == 0:
print("The number is Even.")
else:
print("The number is Odd.")
if sum == no1:
print("Armstrong Number")
else:
print("Not an Armstrong Number")
if num < 0:
print("Factorial is not defined for negative numbers.")
else:
print(f'The factorial of {num} is {factorial_iterative(num)}.')
if result != -1:
print(f'The number {target} is found at index {result}.')
else:
print(f'The number {target} is not found in the list.')
bubble_sort(arr)
print("Sorted array:", arr)
[Space for Output Screenshot]
if result != -1:
print(f'The number {target} is found at index {result}.')
else:
print(f'The number {target} is not found in the list.')
rows1 = int(input("Enter the number of rows for the first matrix: "))
cols1 = int(input("Enter the number of columns for the first matrix: "))
rows2 = int(input("Enter the number of rows for the second matrix: "))
cols2 = int(input("Enter the number of columns for the second matrix: "))
if cols1 != rows2:
print("Matrix multiplication is not possible.")
else:
print("Enter elements for the first matrix:")
mat1 = np.array([[int(input()) for _ in range(cols1)] for _ in range(rows1)])
class Stack:
def __init__(self):
self.items = []
def is_empty(self):
return len(self.items) == 0
def pop(self):
if not self.is_empty():
return self.items.pop()
else:
print("Stack is empty! Cannot pop.")
def peek(self):
if not self.is_empty():
return self.items[-1]
else:
print("Stack is empty! Cannot peek.")
def size(self):
return len(self.items)
def display(self):
if not self.is_empty():
print("Stack elements (top to bottom):", self.items[::-1])
else:
print("Stack is empty!")
stack = Stack()
stack.push(10)
stack.push(20)
stack.push(30)
stack.display()
print("Top element is:", stack.peek())
stack.pop()
stack.display()
class Queue:
def __init__(self):
self.items = []
def is_empty(self):
return len(self.items) == 0
def dequeue(self):
if not self.is_empty():
return self.items.pop(0)
else:
print("Queue is empty! Cannot dequeue.")
def peek(self):
if not self.is_empty():
return self.items[0]
else:
print("Queue is empty! Cannot peek.")
def size(self):
return len(self.items)
def display(self):
if not self.is_empty():
print("Queue elements (front to back):", self.items)
else:
print("Queue is empty!")
queue = Queue()
queue.enqueue(10)
queue.enqueue(20)
queue.enqueue(30)
queue.display()
queue.dequeue()
queue.display()
def print_triangle(n):
for i in range(1, n + 1):
print('*' * i)
def print_inverted_triangle(n):
for i in range(n, 0, -1):
print('*' * i)
n = int(input("Enter the number of rows for the inverted triangle: "))
print_inverted_triangle(n)
def print_pyramid(n):
for i in range(n):
print(' ' * (n - i - 1) + '*' * (2 * i + 1))
def print_diamond(n):
for i in range(n):
print(' ' * (n - i - 1) + '*' * (2 * i + 1))
for i in range(n - 2, -1, -1):
print(' ' * (n - i - 1) + '*' * (2 * i + 1))