Wednesday, 23 September 2015

Program For Pythagorean Triplet


/* 
    Pythagorean Triplet:  
        c*c = a*a + b*b 
*/  
  
public class PythagoreanTriplet  
{  
    public static void main(String s[]) throws Exception  
    {  
        System.out.println("Pythagorean Triplet: ");  
        for(int a=1; a<=50 ; a++)  
        {  
            for(int b=1; b<=50 ; b++)  
            {  
                int csquared = a*a + b*b;  
                double croot = Math.sqrt(csquared);  
  
                if( croot==Math.ceil(croot) )  
                {  
                    System.out.println(a + " " + b + " " + (int)croot);  
                }  
            }  
        }  
    }  
}  
Output:
Pythagorean Triplet:
3 4 5
4 3 5
6 8 10
8 6 10