Generate Pythagorean triplets
C Program to generate Pythagorean triplets with values smaller than given limit (limit is entered by user).
A Pythagorean triplet consists of three positive integers a, b, and c, such that a² + b² = c².
The smallest pythagorean triplet is 3, 4, 5
32 + 42 = 9 + 16 = 25
52 = 25
So, 32 + 42 = 52
Input:
limit = 25
Output:
Pythogorean triplets are
3, 4, 5
5, 12, 13
6, 8, 10
7, 24, 25
8, 15, 17
9, 12, 15
12, 16, 20
15, 20, 25
The smallest pythagorean triplet is 3, 4, 5
32 + 42 = 9 + 16 = 25
52 = 25
So, 32 + 42 = 52
Input:
limit = 25
Output:
Pythogorean triplets are
3, 4, 5
5, 12, 13
6, 8, 10
7, 24, 25
8, 15, 17
9, 12, 15
12, 16, 20
15, 20, 25
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k, t, limit;
printf("Enter the limit within which triplets are to be generated\t:");
scanf("%d", &limit);
printf("Pythagorean triplets are\n");
for (i = 1; i <= limit; i++)
{
for (j = i + 1; j <= limit; j++)
{
t = (i*i) + (j*j);
for (k = i + 2; k <= limit; k++)
{
if (t == k*k)
printf("\t%d, %d, %d \n", i, j, k);
}
}
}
getch();
}
Output
Enter the limit within which triplets are to be generated :45
Pythagorean triplets are
3, 4, 5
5, 12, 13
6, 8, 10
7, 24, 25
8, 15, 17
9, 12, 15
9, 40, 41
10, 24, 26
12, 16, 20
12, 35, 37
15, 20, 25
15, 36, 39
16, 30, 34
18, 24, 30
20, 21, 29
21, 28, 35
24, 32, 40
27, 36, 45