Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Could someone view and verify...  RSS feed

 
Luke Forga
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I seem to be having difficulty in producing an output. It compiles...just doesn't output anything. Where have I gone wrong, may I ask? Thanks in advance!


public class PythagoreanTriples
{

public static void main( String args[] )
{
int side1;
int side2;
int hypotenuse;
int limit = 5;
int i=0;

/* Loop for side1 to try the values 1-limit. */
for (side1=1; side1<limit; ++side1)
{

/* Loop for side2 to try the values 1-limit. */
for (side2=side1+1; side2<limit; ++side2)
{ side2+= side2;

/* Loop for hypotenuse to try the values 1-limit */
for(hypotenuse=side2+1; hypotenuse<limit; ++hypotenuse)
{

/* An if statement that determines whether the sum of the two sides squared equals the hypotenuse squared. If this condition is true display side1, side2 and hypotenuse. */
if(side1*side1+side2*side2 == hypotenuse*hypotenuse)
{
System.out.printf(" %d : ( %d, %d, %d )\n", ++i, side1, side2, hypotenuse);
} // end if
} //end innermost for
} //end inner for
} //end outer for
} //end main

} // end class
 
Ravi Srinivas
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Wonder how you got the following line to compile (seems like a C statement to me)..

Originally posted by Luke Forga:
System.out.printf(" %d : ( %d, %d, %d )\n", ++i, side1, side2, hypotenuse);


Anyway, the reason you see no output is:-

When you reach the innermost for-loop, in the initialisation you set to hypotenuse = side2+1 which on the first run itself turns out to be 5. Since this is not less than the limit 5, you will not enter the inner-most loop. Hence, the print statement is never reached.

If you follow the flow, this is the sequence being executed-
side1 = 1;
side2 = side1+1; //making side2 = 2
side2 += side2; //making side2 = 4
hypotenuse = side2+1; //making hypotenuse = 5, hence not able to enter loop
[ July 20, 2005: Message edited by: Ravi Srinivas ]
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ravi Srinivas:
Wonder how you got the following line to compile (seems like a C statement to me)..


It will compile fine, if you use a Java 1.5 compiler. printf(String format, Object... args) is a new method added in JDK 5.0 to write a formatted string to the output stream
 
Ravi Srinivas
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I see.. thanks for the info
 
Luke Forga
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good deal. I didn't see that "side2+=side2. Silly me. Much appreciated. Thanks again for your assistance.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!