0% found this document useful (0 votes)
9 views5 pages

All 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)
9 views5 pages

All 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/ 5

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]

You might also like