The source code to demonstrate the nested while and do-while loop is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.
In the all below program, we used an object-oriented approach to create the program. We created an object Sample. We defined main() function. The main() function is the entry point for the program.
Nested while loop
// Scala program to demonstrate the
// nested "while" loop
object Sample {
def main(args: Array[String]) {
var cnt1:Int=0;
var cnt2:Int=0;
cnt1=2;
while(cnt1<=5)
{
cnt2=1;
while(cnt2<=10)
{
printf("%d ",(cnt1*cnt2));
cnt2=cnt2+1;
}
cnt1=cnt1+1;
println();
}
}
}
Here, we created two integer variables cnt1, cnt2 initialized with 0. cnt1 is used as counter for outer loop and cnt2 is used for inner loop. The outer loop executes from 2 to 5 and the inner loop will execute 10 times for each number of the outer loop to print the table on numbers on the console screen.
Nested do-while loop
// Scala program to demonstrate the
// nested "do-while" loop
object Sample {
def main(args: Array[String]) {
var cnt1:Int=0;
var cnt2:Int=0;
cnt1=2;
do
{
cnt2=1;
do
{
printf("%d ",(cnt1*cnt2));
cnt2=cnt2+1;
}while(cnt2<=10)
cnt1=cnt1+1;
println();
}while(cnt1<=5)
}
}
Explanation:
Here, we created two integer variables cnt1, cnt2 initialized with 0. cnt1 is used as counter for outer loop and cnt2 is used for inner loop. The outer loop executes from 2 to 5 and the inner loop will execute 10 times for each number of the outer loop to print the table on numbers on the console screen.
Program/Source Code:
The source code to demonstrate the nested while and do-while loop is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.
In the all below program, we used an object-oriented approach to create the program. We created an object Sample. We defined main() function. The main() function is the entry point for the program.
Nested while loop
Output:
Explanation:
Here, we created two integer variables cnt1, cnt2 initialized with 0. cnt1 is used as counter for outer loop and cnt2 is used for inner loop. The outer loop executes from 2 to 5 and the inner loop will execute 10 times for each number of the outer loop to print the table on numbers on the console screen.
Nested do-while loop
Explanation:
Here, we created two integer variables cnt1, cnt2 initialized with 0. cnt1 is used as counter for outer loop and cnt2 is used for inner loop. The outer loop executes from 2 to 5 and the inner loop will execute 10 times for each number of the outer loop to print the table on numbers on the console screen.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer