For a, b, c to be a Pythagorean triplet a*a + b*b should be equals to c*c
i.e. a*a + b*b == c*c
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Scanner; public class PythagoreanTriplet{ public static void main(String []args){ Scanner sc = new Scanner(System.in); int limit = sc.nextInt(); for(int a=1; a<limit; a++) { for(int b=a+1; b<limit; b++) { for(int c=b+1; c<=limit; c++) { if(a*a+b*b==c*c) { System.out.println(a+" "+b+" "+c); break; } } } } } } |
Python
limit = int(input("Enter a limit:")) for a in range(1, limit): for b in range(a+1, limit): for c in range(b+1, limit+1): if a**2+b**2==c**2: print(a,b,c) break |