929c9600-9710-4158-b8bc-9a8f3cdb1452_4_Nested Loop-Lesson 0
929c9600-9710-4158-b8bc-9a8f3cdb1452_4_Nested Loop-Lesson 0
1.
int weeks = 3;
int days = 7;
for (int i = 1; i <= weeks; ++i)
{
System.out.println("Week: " + i);
for (int j = 1; j <= days; ++j)
{
System.out.println(" Day: " + j);
}
}
int rows = 5;
for (int i = 1; i <= rows; ++i)
{
for (int j = 1; j <= i; ++j)
{
System.out.print(j + " ");
}
System.out.println("");
}
for (int row = 1; row <= 5; row++) {
System.out.print("*");
for (int col = 1; col < 5; col++) {
System.out.print(" *");
}
System.out.println();
}
Week: 1
Day: 1
Day: 2
Day: 3
Day: 4
Day: 5
Day: 6
Day: 7
Week: 2
Day: 1
Day: 2
Day: 3
Day: 4
Day: 5
Day: 6
Day: 7
Week: 3
Day: 1
Day: 2
Day: 3
Day: 4
Day: 5
Day: 6
Day: 7
1
1 2
1 23
1 234
1 2345
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
When a loop contains another loop in its body, then it is said
to be a Nested Loop. Learning to use the Nested loop is
important because there are a wide range of programming
scenarios that require the application of a nested loop.
case 3:
System.out.println("Enter the ending number:");
int n=sc.nextInt();
int sum=0;
float avg;
int u;
for(u=1;u<=n;u++)
{
sum+=u;
}
avg=(float)sum/u;
System.out.println("Average of " + n+ "numbers is:" +avg);
break;
case 4:
System.exit(0);
break;
default:
System.out.println("Invalid input");
}//switch
}while(true);
}//main
}//class
CLOSURE