Number of Iterations

Problem

If you run the following block of java code. How many times the print statement is going to execute.

Solution

You can modify the code by adding a counter then let the code itself calculates how many times it
executes however that is not what the problem is all about. You can trace the code but this is a
time consuming process. In order to find an exact number regardless of the loop limits you need
to use a counting technique. Consider the two nested loops as a nested sum as follows:

Tags:

Add a Comment

Your email address will not be published. Required fields are marked *