Python Programming/Basic Math/Solutions

< 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)))
Last modified on 29 November 2011, at 23:40