In: Computer Science
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf())
Complete the following functions using C programming language:
(Pythagorean Triples) A right triangle can have sides that are all integers. The set of three integer values for the sides of a right triangle is called a Pythagorean triple. These three sides must satisfy the relationship that the sum of the squares of two of the sides is equal to the square of the hypotenuse.
Complete function intQ5() to find all Pythagorean triples for side1, side2 and the hypotenuse all no larger than 400, with side1<= side2. Use a triple-nested for loop that simply tries all possibilities.
This is an example of the “brute-force” approach. You should print each triple on a separate line using the format: printf("A triple found: side1=%d, side2=%d, side3=%d\n", ...). Finally, you must return the total number of triples at the end.
#include<stdio.h> void intQ5(){ int i,j,k; for(i=1;i<=400;i++){ for(j=1;j<=i;j++){ for(k=0;k<=400;k++){ if(i*i+j*j==k*k){ printf("A triple found: side1=%d, side2=%d, side3=%d\n", i,j,k); } } } } } int main(){ intQ5(); return 0; }
void intQ5(){ int i,j,k; for(i=1;i<=400;i++){ for(j=1;j<=i;j++){ for(k=0;k<=400;k++){ if(i*i+j*j==k*k){ printf("A triple found: side1=%d, side2=%d, side3=%d\n", i,j,k); } } } } }