Skip to content

Commit

Permalink
Create lab1_q6
Browse files Browse the repository at this point in the history
  • Loading branch information
ZanButt authored Jan 9, 2021
1 parent 46e9317 commit 72e543e
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lab1_q6
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*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.
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.
Your program should print each triple on a separate line, and the total number of triples at the end*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{ /* program takes all pythagorean triplets
from 0 to 400 and prints them */
int side1,side2,side3,sum=0;
// 3 for loops are iterated through from 0 to 400 in order to help find the pytagorean triplets
for(side1=1;side1<=400;side1++){
for(side2=1;side2<=400;side2++){
for(side3=1;side3<=400;side3++){
if(side1*side1+side2*side2 == side3*side3 && side1<=side2){ /* if side1^2 + side2^2 = s
ide3^2 (and they are equal to an integer) then sum will increase by 1
and the 3 sides will be printed*/
sum +=1;
printf("%d,%d,%d\n",side1,side2,side3); // prints all 3 side
}
}

}

}
printf("%d",sum);
return 0;
}

0 comments on commit 72e543e

Please sign in to comment.