Tuesday, February 14, 2012

BlueJ Program on Pythagorean triplets Display


Pythagorean triplet consists of three positive integers namely a,b and c in such a way that a2 + b2 = c2. Some examples of Pythagorean triplets are :

( 3 , 4 , 5 )
( 5, 12, 13)
( 7, 24, 25)
( 8, 15, 17)
( 9, 40, 41)
(11, 60, 61)
(12, 35, 37)
(13, 84, 85)
(16, 63, 65)
(20, 21, 29)
(28, 45, 53)
(33, 56, 65)
(36, 77, 85)
(39, 80, 89)
(48, 55, 73)
(65, 72, 97)

 Here is the bluej program where the Pythagorean triplets staring from 1 to 50 are displayed.

class Max
{
int i,j,k,a,b,c,m=50;
public void disp()
{
System.out.println("The Pythogorean triplets are");
for(i=1;i< m-2;i++)
{
for(j=i+1;j< m-1;j++)
{
 for(k=j+1;k< m;k++)
 {
a=i;
b=j;
c=k;
if(a*a+b*b==c*c)
System.out.println("("+a+ ","+b+","+c+")");
}
}
}
}
public static void main(String args[])
{
 Max ob=new Max();
 ob.disp();
}
}

Sample output:

The Pythogorean triplets are
(3,4,5)
(5,12,13)
(6,8,10)
(7,24,25)
(8,15,17)
(9,12,15)
(9,40,41)
(10,24,26)
(12,16,20)
(12,35,37)
(15,20,25)
(15,36,39)
(16,30,34)
(18,24,30)
(20,21,29)
(21,28,35)
(24,32,40)
(27,36,45)


Back To BlueJ Series Display Programs Home Page: CLICK HERE

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner