forked from Abraarkhan/Java_Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpythagoreanTriplet.java
63 lines (52 loc) · 1.34 KB
/
pythagoreanTriplet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
public class triplet
{
//Check if give numbers are pythagorean triplet or not
//body
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter three numbers");
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
if(a==b && b==c)
{
System.out.println(a + " , " + b + " and " + c + " are not pythagorean triplet");
}
else
{
if(check(a,b,c))
{
System.out.println(a + " , " + b + " and " + c + " are pythagorean triplet");
}
else
{
System.out.println(a + " , " + b + " and " + c + " are not pythagorean triplet");
}
}
}
//function
static boolean check(int a , int b , int c )
{
int l = larg(a,b,c);
if(l == a)
{
return l*l == (b*b)+(c*c);
}
else if(l == b )
{
return l*l == (a*a)+(c*c);
}
else
{
return l*l == (a*a)+(b*b);
}
}
//function to find the largest
static int larg(int a,int b ,int c)
{
int x = Math.max(a,b);
x = Math.max(x,c);
return x;
}
}