Pythagorean Triples C Program

Pythagorean Triples


Level: Easy

Three numbers form a Pythagorean triple if the sum of squares of two numbers is equal to the square of the third.

 For example, 3, 5 and 4 form a Pythagorean triple, since 3*3 + 4*4 = 25 = 5*5

You are given three integers, a, b, and c. They need not be given in increasing order. If they form a Pythagorean triple, then print "yes", otherwise, print "no". Please note that the output message is in small letters.

Sample Input
3
5
4

Sample Output
yes


InputOutput
Test Case 1
3
5
4
yes
Test Case 2
5
8
2
no
Test Case 3
5
12
13
yes
Test Case 4
50
130
120
yes
Test Case 5
120
130
50
yes
Test Case 6
9999
9801
1980
yes
Test Case 7
5
8
12
no

Source Code:

#include <stdio.h>

int main()
{
 int i, j, k;             /* inputs */
 int i_sq, j_sq, k_sq;    /* squares of inputs */
 
 scanf("%d",&i);
 scanf("%d",&j);
 scanf("%d",&k);
 
        /* compute the squares of inputs */
 i_sq = i*i;
 j_sq = j*j;
 k_sq = k*k;
 
        /* check if some ordering of the inputs
         * form a Pythagorean triple
         */
 if(i_sq+j_sq == k_sq || 
    j_sq+k_sq == i_sq ||
    i_sq+k_sq == j_sq){
    printf("yes");
 }else{
    printf("no");
 }
}


/*-------------------------------------------
 * Additional comments:
 *  The only thing to note here is the way 
 *  the if condition was written. We do not 
 *  know in what order i,j,k satisfy the 
 *  condition we need. 
 *    So we have to check for the three 
 *  possibilities.
 *
 *    Note that only the largest number can
 *  be on the right hand side of the equation
 *   a*a + b*b = c*c
 *  You can use this observation for another 
 *  solution to the problem. However, in this 
 *  case,you have to find the maximum of the three
 *  numbers.
 */

Labels: ,