< Python Programming < Basic Math

< Back to Problems

1. Ask the user to specify the number of sides on a polygon and find the number of diagonals within the polygon.

import math
poligonos = int(input("Especifica el numero de lados del Poligono: " ))
n = int(poligonos)
diagonales = int( (n*(n-3))/2)
print("El Pligono de "+str(poligonos)+" lados, tiene "+str(diagonales)+" diagonales")

2. Take the lengths of two sides to a triangle from the user and apply the Pythagorean Theorem to find the third.

print("Introduce el Lado A y B del triangulo para obtener el lado C.")
ladoA = int(input("Introduce Lado A: "))
ladoB = int(input("Introduce Lado B: "))
a = ladoA
b = ladoB
ladoC = math.sqrt( (a**2 + b**2) )
c = ladoC
print("El lado C es: "+str(c))
print("El lado C es: "+str(round(c, 2)))
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.