-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab1_q6
34 lines (29 loc) · 1.47 KB
/
lab1_q6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*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;
}