Program: 115
Write a c program to generate all Pythagorean Triplets with side length less than or equal to 30.
a2 + b2 = c2
#include<stdio.h> #include<conio.h> #include<math.h> int main() { int a, b; float c; //calculate the another side using Pythagoras Theorem //a*a + b*b = c*c //c = sqrt(a*a+b*b) //maximum length should be equal to 30 for(a=1;a<=30;a++) { for(b=1;b<=30;b++) { c = sqrt(a*a+b*b); if(c == (int)c) { printf("(%d, %d, %d)\n",a,b,(int)c); } } } }
Output:
(3, 4, 5) (4, 3, 5) (5, 12, 13) (6, 8, 10) (7, 24, 25) (8, 6, 10) (8, 15, 17) (9, 12, 15) (10, 24, 26) (12, 5, 13) (12, 9, 15) (12, 16, 20) (15, 8, 17) (15, 20, 25) (16, 12, 20) (16, 30, 34) (18, 24, 30) (20, 15, 25) (20, 21, 29) (21, 20, 29) (21, 28, 35) (24, 7, 25) (24, 10, 26) (24, 18, 30) (28, 21, 35) (30, 16, 34)
Leave a Comment