The Pythagorean theorem states
that the
sum of the squares of the sides of a right triangle is equal to the
square of the hypotenuse. For example, if two sides of a right
triangle have lengths of 3 and 4, then the hypotenuse must have a
length of 5. Together the integers 3, 4, and 5 form a Pythagorean
triple. There are an infinite number of such triples. Given two
positive integers, m and n,
where
m > n, a Pythagorean
triple can be generated by the following formulas: side1
= m2 - n2
side2 = 2mn hypotenuse = m2 + n2
For example, the triple ( side1 = 3, side2 = 4, hypotenuse = 5) is
generated by this formula when m
= 2 and n = 1. |
Consider the letters of the alphabet as being numbered according to
their position, A=1, B=2, etc. Write a program which accepts two
initials
from the user, computes the corresponding integers, and then outputs
the
Pythagorean triple for those integers.
Functions: findSide1(), findSide2(), findSide3()
Algorithm: Determine the three parts of the triple, according to the
formulas provided above.
Algorithm:
number1 = (int) (initial1 - 'A') + 1;
number2 = (int) (initial2 - 'A') + 1;