0% found this document useful (0 votes)
5 views11 pages

Triangle Area and Number Comparison Methods

Uploaded by

rsg189423114
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views11 pages

Triangle Area and Number Comparison Methods

Uploaded by

rsg189423114
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Program No-1:

import math

# Method 1: Calculate triangle area in a simple way


def simple_triangle_area():
print("\nMethod 1: Calculating triangle area in a simple way")
# Taking input
a = float(input("Length of first side: "))
b = float(input("Length of second side: "))
c = float(input("Length of third side: "))

# Check if a triangle can be formed


if (a + b > c) and (b + c > a) and (a + c > b):
# Calculate semi-perimeter (s)
s = (a + b + c) / 2
# Heron's formula
area = [Link](s * (s - a) * (s - b) * (s - c))
print(f"Area of the triangle: {area:.2f} square units")
else:
print("A triangle cannot be formed with the given sides.")

# Method 2: Calculate triangle area using a function


def function_triangle_area(a, b, c):
print("\nMethod 2: Calculating triangle area using a function")
# Check if a triangle can be formed
if (a + b > c) and (b + c > a) and (a + c > b):
# Calculate semi-perimeter (s)
s = (a + b + c) / 2
# Heron's formula
area = [Link](s * (s - a) * (s - b) * (s - c))
return area
else:
return None

# Method 3: Calculate triangle area using a class


class Triangle:
def __init__(self, a, b, c):
self.a = a # First side
self.b = b # Second side
self.c = c # Third side

def is_valid(self):
# Check if a triangle can be formed
return (self.a + self.b > self.c) and (self.b + self.c > self.a) and
(self.a + self.c > self.b)

def calculate_area(self):
print("\nMethod 3: Calculating triangle area using a class")
if self.is_valid():
# Calculate semi-perimeter (s)
s = (self.a + self.b + self.c) / 2
# Heron's formula
area = [Link](s * (s - self.a) * (s - self.b) * (s - self.c))
return area
else:
return None

# Main program
def main():
print("Program to Calculate the Area of a Scalene Triangle")
print("---------------------------------------------")

# Taking input (taken once and used for all methods)


a = float(input("Length of first side: "))
b = float(input("Length of second side: "))
c = float(input("Length of third side: "))

# Method 1: Simple way


simple_triangle_area()

# Method 2: Using function


result_function = function_triangle_area(a, b, c)
if result_function is not None:
print(f"Area of the triangle: {result_function:.2f} square units")
else:
print("A triangle cannot be formed with the given sides.")

# Method 3: Using class


triangle = Triangle(a, b, c)
result_class = triangle.calculate_area()
if result_class is not None:
print(f"Area of the triangle: {result_class:.2f} square units")
else:
print("A triangle cannot be formed with the given sides.")

# Run the program


if __name__ == "__main__":
main()
Output No-1:

Program to Calculate the Area of a Scalene Triangle


---------------------------------------------
Length of first side: 8
Length of second side: 5
Length of third side: 4

Method 1: Calculating triangle area in a simple way


Length of first side: 8
Length of second side: 5
Length of third side: 4
Area of the triangle: 8.18 square units

Method 2: Calculating triangle area using a function


Area of the triangle: 8.18 square units

Method 3: Calculating triangle area using a class


Area of the triangle: 8.18 square units
PS C:\Users\user>
Program No-2:

# Python program to find the smallest and largest number

# 1. Simple Method
def simple_method():
print("\n--- Simple Method ---")
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

if num1 > num2:


large = num1
small = num2
else:
large = num2
small = num1

print(f"Largest number: {large}")


print(f"Smallest number: {small}")

# 2. Function-Based Method
def function_method():
print("\n--- Function Method ---")
def find_large_small(num1, num2):
if num1 > num2:
return num1, num2 # Largest, Smallest
else:
return num2, num1 # Largest, Smallest

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


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

large, small = find_large_small(num1, num2)


print(f"Largest number: {large}")
print(f"Smallest number: {small}")

# 3. Class-Based Method
class NumberComparator:
def _init_(self, num1, num2):
self.num1 = num1
self.num2 = num2

def find_large_small(self):
if self.num1 > self.num2:
return self.num1, self.num2 # Largest, Smallest
else:
return self.num2, self.num1 # Largest, Smallest

def class_method():
print("\n--- Class Method ---")
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

comparator = NumberComparator(num1, num2)


large, small = comparator.find_large_small()

print(f"Largest number: {large}")


print(f"Smallest number: {small}")

# Main Program
def main():
while True:
print("\nProgram to Find Smallest and Largest Number")
print("1. Simple Method")
print("2. Function Method")
print("3. Class Method")
print("4. Exit Program")

choice = input("Select your choice (1-4): ")

if choice == '1':
simple_method()
elif choice == '2':
function_method()
elif choice == '3':
class_method()
elif choice == '4':
print("Program ended. Thank you!")
break
else:
print("Invalid choice! Please select between 1-4.")

# Run the Program


if __name__ == "__main__":
main()
Output No-2:

Program to Find Smallest and Largest Number


1. Simple Method
2. Function Method
3. Class Method
4. Exit Program
Select your choice (1-4): 1

--- Simple Method ---


Enter the first number: 11
Enter the second number: 9
Largest number: 11.0
Smallest number: 9.0

Program to Find Smallest and Largest Number


1. Simple Method
2. Function Method
3. Class Method
4. Exit Program
Select your choice (1-4): 4
Program ended. Thank you!
PS C:\Users\user>
Program No-3:

import math

# Simple method to solve quadratic equation


def simple_quadratic_solver(a, b, c):
# Calculate discriminant
discriminant = b**2 - 4*a*c
if a == 0:
return "This is not a quadratic equation!"
elif discriminant > 0:
root1 = (-b + [Link](discriminant)) / (2*a)
root2 = (-b - [Link](discriminant)) / (2*a)
return f"Two roots: {root1:.2f} and {root2:.2f}"
elif discriminant == 0:
root = -b / (2*a)
return f"One root: {root:.2f}"
else:
return "No real roots exist"

# Function-based method to solve quadratic equation


def quadratic_function_solver(a, b, c):
# Calculate discriminant
discriminant = b**2 - 4*a*c
if a == 0:
return "This is not a quadratic equation!"
elif discriminant > 0:
root1 = (-b + [Link](discriminant)) / (2*a)
root2 = (-b - [Link](discriminant)) / (2*a)
return f"Two roots: {root1:.2f} and {root2:.2f}"
elif discriminant == 0:
root = -b / (2*a)
return f"One root: {root:.2f}"
else:
return "No real roots exist"

# Class-based method to solve quadratic equation


class QuadraticEquation:
def _init_(self, a, b, c):
self.a = a
self.b = b
self.c = c

def find_roots(self):
# Calculate discriminant
discriminant = self.b**2 - 4*self.a*self.c
if self.a == 0:
return "This is not a quadratic equation!"
elif discriminant > 0:
root1 = (-self.b + [Link](discriminant)) / (2*self.a)
root2 = (-self.b - [Link](discriminant)) / (2*self.a)
return f"Two roots: {root1:.2f} and {root2:.2f}"
elif discriminant == 0:
root = -self.b / (2*self.a)
return f"One root: {root:.2f}"
else:
return "No real roots exist"

# Main program
def main():
print("Quadratic Equation Solver")
print("-------------------------")
print("Choose a method:")
print("1. Simple Method")
print("2. Function Method")
print("3. Class Method")

choice = input("Enter your choice (1/2/3): ")

# Input coefficients
try:
a = float(input("Enter the value of a: "))
b = float(input("Enter the value of b: "))
c = float(input("Enter the value of c: "))
except ValueError:
print("Please enter valid numbers!")
return

# Display result based on choice


if choice == '1':
print("\nResult from Simple Method:")
print(simple_quadratic_solver(a, b, c))
elif choice == '2':
print("\nResult from Function Method:")
print(quadratic_function_solver(a, b, c))
elif choice == '3':
print("\nResult from Class Method:")
equation = QuadraticEquation(a, b, c)
print(equation.find_roots())
else:
print("Invalid choice! Please select 1, 2, or 3.")

# Run the program


if __name__ == "__main__":
main()
Output No-3:

Quadratic Equation Solver


-------------------------
Choose a method:
1. Simple Method
2. Function Method
3. Class Method
Enter your choice (1/2/3): 1
Enter the value of a: 1
Enter the value of b: -3
Enter the value of c: 2

Result from Simple Method:


Two roots: 2.00 and 1.00
PS C:\Users\user>
Weather Report:

import requests

def get_weather(city):
api_key = "69f5f380e8038fd7a8d22727eae72aa5"
url =
f"[Link]
etric&lang=en"

print("Searching weather...\n")

try:
response = [Link](url)
data = [Link]()

if data["cod"] != 200:
print("City not found or API error!")
return

city_name = data["name"]
country = data["sys"]["country"]
temp = data["main"]["temp"]
description = data["weather"][0]["description"]
feels_like = data["main"]["feels_like"]
humidity = data["main"]["humidity"]

print(f"Location: {city_name}, {country}")


print(f"Temperature: {temp}°C (Feels like {feels_like}°C)")
print(f"Weather: {[Link]()}")
print(f"Humidity: {humidity}%")
print("Have a nice day!")

except Exception as e:
print(f"Error: {e}")

# user input
city_input = input("Enter the name of the city: ")
get_weather(city_input)

Output:

Enter the name of the city: dhaka


Searching weather...
Location: Dhaka, BD
Temperature: 33.14°C (Feels like 39.88°C)
Weather: Broken clouds
Humidity: 60%
Have a nice day!

Enter the name of the city: feni


Searching weather...

Location: Feni, BD
Temperature: 29.35°C (Feels like 34.8°C)
Weather: Broken clouds
Humidity: 76%
Have a nice day!

You might also like