Python

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Program:

principal = float(input("Enter the principal amount: "))


rate = float(input("Enter the rate of interest: "))
time = float(input("Enter the time in years: "))
compound_interest = principal * ((1 + rate/100) ** time) - principal
print("The compound interest is:", compound_interest)

output:
Program:

import math

def calculate_area_in_acres(radius_in_feet):
area_in_acres = math.pi * (radius_in_feet**2) / 43560
return area_in_acres

# Input the radius of the field in feet


radius_in_feet = float(input("Enter the radius of the field in feet: "))

# Calculate and print the area of the field in acres


area_in_acres = calculate_area_in_acres(radius_in_feet)
print(f"The area of the field is: {area_in_acres:.2f} acres")

output:
Program:

# Compute the area of a field, reporting the result in acres.


SQFT_PER_ACRE = 43560

# Read the dimensions from the user


length = float(input("Enter the length of the field in feet: "))
width = float(input("Enter the width of the field in feet: "))

# Compute the area in acres


acres = length * width / SQFT_PER_ACRE

# Display the result


print("The area of the field is", acres, "acres")

output:
Program:

import math

def calculate_distance(x1, y1, x2, y2):


distance = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
return distance

# Input the coordinates of Ram


x_ram = float(input("Enter the X-coordinate of Ram: "))
y_ram = float(input("Enter the Y-coordinate of Ram: "))

# Input the coordinates of Sita


x_sita = float(input("Enter the X-coordinate of Sita: "))
y_sita = float(input("Enter the Y-coordinate of Sita: "))

# Calculate and print the distance between Ram and Sita


distance_between_them = calculate_distance(x_ram, y_ram, x_sita, y_sita)
print(f"The distance between Ram and Sita is: {distance_between_them:.2f}")

output:
program:

import random

def guess_number():

target_number=random.randint(1,9)

while True:

user_guess=int(input("guess a number betweeen 1 and 9:"))

if user_guess==target_number:

print("well guessed!")

break

else:

print("wrong guess.try agian")

guess_number()

output:
program:
for number in range(1, 51):

if number % 3 == 0 and number % 5 == 0:

print("FizzBuzz")

elif number % 3 == 0:

print("Fizz")

elif number % 5 == 0:

print("Buzz")

else:

print(number)

output:
program:
nums = list(input("Enter a sequence of comma-separated 4-digit binary numbers: ").split(','))

res = [i for i in nums if int(i, 2)%6==0]

print(res)

output:
program:
def count_even_odd(numbers):

even_count = 0

odd_count = 0

for num in numbers:

if num % 2 == 0:

even_count += 1

else:

odd_count += 1

return even_count, odd_count

input_numbers = input("Enter a series of numbers separated by spaces: ")

numbers = list(map(int, input_numbers.split()))

even_count, odd_count = count_even_odd(numbers)

print("Number of even numbers:", even_count)

print("Number of odd numbers:", odd_count)

output:
Program:

N = int(input("enter size of matrix"))


A = []
B = []
# read matrix A
print("enter A matrix values")
for i in range(N):
row = list(map(int, input().split()))
A.append(row)
# read matrix B
print("enter B matrix values")
for i in range(N):
row = list(map(int, input().split()))
B.append(row)
# initialize matrix C
C = [[0 for j in range(N)] for i in range(N)]

# calculate C = AB
for i in range(N):
for j in range(N):
sum = 0
for k in range(N):
sum += A[i][k] * B[k][j]
C[i][j] = sum

# print matrix C
for row in C:
print(row)

output:
Program:
def wobniar():
# Local variable with the colors of the rainbow
rainbow = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']

# Create an empty list to store the selected and reversed colors


wobniar_list = []

# Iterate through every other color starting at index 0


for i in range(0, len(rainbow), 2):
# Get the color at the current index
color = rainbow[i]
# Reverse the color string
reversed_color = color[::-1]
# Add the reversed color to the new list
wobniar_list.append(reversed_color)

# Print the new list with reversed colors


print(wobniar_list)

# Return the new list


return wobniar_list

# Call the function


wobniar()

output:
Program:
n = int(input("Enter the number of dictionaries: "))
my_dict = {}

for i in range(n):
key = int(input("Enter the key for dictionary {}: ".format(i+1)))
value = int(input("Enter the value for dictionary {}: ".format(i+1)))

my_dict[key] = value

print("The dictionary is")


print(my_dict)

output:
Program:

# Read the number of elements in the list


n = int(input("Enter the number of elements in the list: "))

# Create an empty list


lst = []

# Read the elements of the list from the user and append them to the list
for i in range(n):
elem = int(input("Enter element " + str(i+1) + ": "))
lst.append(elem)

# Swap the first and last element of the list


lst[0], lst[-1] = lst[-1], lst[0]

# Print the new list after swapping


print("New list is:", lst)

output:
Program:
T = int(input())
for t in range(T):
N, K, P = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
count = 0
for i in range(1, N+1):
if i not in A:
count += 1
if count == P:
print(i)
break
if P > N-K:
print(-1)

output:
Program:

def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]

n = int(input("Enter the number of integers: "))


arr = []

for i in range(n):
arr.append(int(input("Enter an integer: ")))

bubble_sort(arr)
print("Sorted list: ", arr)

print("Smallest number: ", arr[0])


print("Largest number: ", arr[-1])

output:

You might also like