First Solution
#include<stdio.h>
main(){
int initial,final,a,b,c;
printf("Enter the range in which you want to search for Pythagorean Triplets:\nInitial: ");
scanf("%d",&initial);
printf("\nFinal: ");
scanf("%d",&final);
printf("The Pythogorean Triplets in the given range are as follows:\n____________________________________________________________\n");
for(a=initial;a<=final;a++){
for(b=a;b<=final;b++){
for(c=b;c<=final;c++){
if(c*c==a*a+b*b){
printf("%d , %d , %d\n",a,b,c);
}
}
}
}
}
second solution
void
pythagoreanTriplets(
int
limit)
{
int
a, b, c = 0;
int
m = 2;
while
(c < limit) {
for
(
int
n = 1; n < m; ++n) {
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++;
}
}
int
main()
{
int
limit = 20;
pythagoreanTriplets(limit);
return
0;
}
o/p
3 , 4 , 5
5 , 12 , 13
6 , 8 , 10
8 , 15 , 17
9 , 12 , 15
12 , 16 , 20
No comments:
Post a Comment