Question: Nested Loops: Loops within loops Predict the output: 1. for (int i = 1; i
Nested Loops: Loops within loops
Predict the output:
1. for (int i = 1; i <= 3; i ++)
{
for (int j = 1; j <=2; j++)
{
System.out.println (i + + j);
}
}
2. Which loop executes more times, the inner or the outer?
3. Predict the output:
int x = 5;
for (int j = 1; j <= x; j++)
{
System.out.println (j);
x = 10;
}
4. Write a nested for loop for the following sequence:
What pattern do you see?
How many loops are there?
Which is the inner? Which is the outer?
1123212331234123
5. Write a nested for loop for the following sequence:
111234121234211234221234311234321234
6. Trace and predict the output for:
for (int i = 0; i <= 10; i = i + 2)
{ System.out.println (i*i); }
7. Trace and predict the value of j at the end of the loop:
int j = 10;
for (int i = 10; i >= 0; i--)
{
j--;
}
8. Trace and predict the value of n at the end of the loop:
int n = 0;
for (int i = 1; i <= 5; i++)
{
for (int j = 0; j < i; j++)
{ n = n + j;
}
}
9. Program: Write a for loop that will take a value from the user and print out the even numbers from 2 to the entered value.
10. Program: Write a for loop that will take a value from the user and print out the multiples of 5 from 5 to the number.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
