0% found this document useful (0 votes)
6 views8 pages

Generated 14 Programs

Uploaded by

chessaadil
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)
6 views8 pages

Generated 14 Programs

Uploaded by

chessaadil
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
You are on page 1/ 8

Program 1: Calculate the Sum of Two Numbers

# Program to calculate the sum of two numbers


num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

# Calculate the sum


sum_result = num1 + num2

print("The sum of the numbers is:", sum_result)

[Space for Output Screenshot]

Program 2: Check if a Number is Even or Odd

# Program to check if a number is even or odd


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

if number % 2 == 0:
print("The number is Even.")
else:
print("The number is Odd.")

[Space for Output Screenshot]

Program 3: Check if the Entered Number is Armstrong or Not

# Program to check if the entered number is Armstrong or not


# An Armstrong number has the sum of the cubes of its digits equal to the number itself

no1 = int(input("Enter any number to check: "))


no = no1 # Copy of the number
sum = 0
while no > 0:
ans = no % 10
sum += ans ** 3
no //= 10

if sum == no1:
print("Armstrong Number")
else:
print("Not an Armstrong Number")

[Space for Output Screenshot]

Program 4: Find the Factorial of the Entered Number

# Program to find the factorial of a number


def factorial_iterative(n):
result = 1
for i in range(1, n + 1):
result *= i
return result

num = int(input("Enter a non-negative integer: "))

if num < 0:
print("Factorial is not defined for negative numbers.")
else:
print(f'The factorial of {num} is {factorial_iterative(num)}.')

[Space for Output Screenshot]

Program 5: Perform Binary Search

# Program for binary search


def binary_search(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = left + (right - left) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1

arr = list(map(int, input("Enter sorted numbers separated by spaces: ").split()))


target = int(input("Enter the number to search for: "))

result = binary_search(arr, target)

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.')

[Space for Output Screenshot]

Program 6: Bubble Sort

# Program to perform Bubble Sort


def bubble_sort(arr):
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] # Swap

arr = list(map(int, input("Enter numbers separated by spaces: ").split()))

bubble_sort(arr)
print("Sorted array:", arr)
[Space for Output Screenshot]

Program 7: Linear Search

# Program to perform Linear Search


def linear_search(arr, target):
for i, element in enumerate(arr):
if element == target:
return i
return -1

arr = list(map(int, input("Enter numbers separated by spaces: ").split()))


target = int(input("Enter the number to search for: "))

result = linear_search(arr, target)

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.')

[Space for Output Screenshot]

Program 8: Matrix Multiplication

# Program to perform Matrix Multiplication


import numpy as np

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)])

print("Enter elements for the second matrix:")


mat2 = np.array([[int(input()) for _ in range(cols2)] for _ in range(rows2)])

result = np.dot(mat1, mat2)


print("Resultant Matrix:")
print(result)

[Space for Output Screenshot]

Program 9: Stack Implementation

class Stack:
def __init__(self):
self.items = []

def is_empty(self):
return len(self.items) == 0

def push(self, item):


self.items.append(item)
print(f'Pushed {item} to the stack.')

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()

[Space for Output Screenshot]

Program 10: Queue Implementation

class Queue:
def __init__(self):
self.items = []

def is_empty(self):
return len(self.items) == 0

def enqueue(self, item):


self.items.append(item)
print(f'Enqueued {item} to the queue.')

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()

[Space for Output Screenshot]

Program 11: Triangle Pattern

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

n = int(input("Enter the number of rows for the triangle: "))


print_triangle(n)

[Space for Output Screenshot]

Program 12: Inverted Triangle Pattern

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)

[Space for Output Screenshot]

Program 13: Pyramid Pattern

def print_pyramid(n):
for i in range(n):
print(' ' * (n - i - 1) + '*' * (2 * i + 1))

n = int(input("Enter the number of rows for the pyramid: "))


print_pyramid(n)

[Space for Output Screenshot]

Program 14: Diamond Pattern

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))

n = int(input("Enter the number of rows for the diamond: "))


print_diamond(n)

[Space for Output Screenshot]

You might also like