For the following method and method calls write what will be

For the following method and method calls, write what will be printed to the console when the method call is executed.

Solution

//This will print the values starting from i, till j, in a way such that, the increment
//value increases linearly from 1, and will continue, till the value reaches j.
//So, the series will be, i, i+1, i+1+2, i+1+2+3, i+1+2+3+4,.....
public static void whileMystery(int i, int j)
{
int k = 0;       //Defines a variable k and will assign to 0.
while(i < j && k < j)   //This loop runs till both i and k values are both less than j.
{
i = i + k;           //i increased by a step of k.
j--;                   //j is reduced by 1.
k++;                   //k is increased by 1.
System.out.print(i + \", \");   //Prints i value.
}
System.out.println(k);    //Prints k value.
}

For example if the function is called with parameters whileMystery(2, 10), the output is: 2, 3, 5, 8. And the final k will print the number of values printed, i.e., 4 in this case.

 For the following method and method calls, write what will be printed to the console when the method call is executed. Solution//This will print the values sta

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site