0
Write a Program to print 15 Pythagoras Triplets.
Use three Loops(can be For)Innermost loop will have Break with label.Introduce label to the outermost loop to exit when ten triplets get printed.
2 odpowiedzi
+ 2
http://code.sololearn.com/cezMK9e2bD1R
public class Program {
public static void main(String[] args) {
int count = 0;
search:
for (int c = 3; ; c++) {
for (int b = 2; b < c; b++) {
for (int a = 1; a < b; a++) {
if (a*a + b*b == c*c) {
System.out.println("(" + a + ", " + b + ", " + c + ")");
count++;
if (count >= 15) {
break search;
}
}
}
}
}
}
}
0
actually, there are only three pythagorean triplets in mathematics i.e 3, 4 and 5