You don't need two loops. Just use one while loop with two variables:
int first = 0;
int last = length - 1;
while (first <= last)
{
if (first < last)
System.out.printf("%d. sum: %d%n", first + 1, array[first] + array[last]);
else
System.out.printf("%d. mid: %d%n", first + 1, array[first]);
first++;
last--;
}
You might also use a
for
loop instead (iterating over the half length) and calculate the last index inside the loop:
for (int first = 0; first < (length + 1) / 2; first++)
{
int last = length - 1 - first;
if (first < last)
System.out.printf("%d. sum: %d%n", first + 1, array[first] + array[last]);
else
System.out.printf("%d. mid: %d%n", first + 1, array[first]);
}