how to write a program to check whether it is a pythagorean tirplet or not in c++
Answers
Answer:
// C++ program to generate pythagorean
// triplets smaller than a given limit
Explanation:
#include <bits/stdc++.h>
// Function to generate pythagorean
// triplets smaller than limit
void pythagoreanTriplets(int limit)
{
// triplet: a^2 + b^2 = c^2
int a, b, c = 0;
// loop from 2 to max_limitit
int m = 2;
// Limiting c would limit
// all a, b and c
while (c < limit) {
// now loop on j from 1 to i-1
for (int n = 1; n < m; ++n) {
// Evaluate and print triplets using
// the relation between a, b and c
a = m * m - n * n;
b = 2 * m * n;
c = m * m + n * n;
if (c > limit)
break;
printf("%d %d %d\n", a, b, c);
}
m++;
}
}
// Driver Code
int main()
{
int limit = 20;
pythagoreanTriplets(limit);
return 0;
}
=============================================================
Output :
3 4 5
8 6 10
5 12 13
15 8 17
12 16 20