Chapter 9 Nested Loop
Chapter 9 Nested Loop
Nested Loop
Class 10 - APC Understanding Computer Applications
with BlueJ
Question 1
1. double loop
2. embedded loop
3. circular loop
4. nested loop
Answer
nested loop
Question 2
The ............... break is used to terminate the outer loop from the block of inner loop.
1. level
2. labelled
3. unlabelled
4. forced
Answer
labelled
Reason — The labelled break is used to terminate the outer loop from the block of inner loop.
Question 3
Which of the following keywords can be used to terminate a switch case as well as a loop construct?
1. continue
2. void
3. break
4. stop
Answer
break
Reason — break can be used to terminate a switch case as well as a loop construct.
Question 4
1. 12 times
2. 8 times
3. 4 times
4. 16 times
Answer
12 times
Reason — The outer loop will execute 4 times. For each iteration of outer loop, the inner loop will execute 3 times.
Therefore, total iterations of nested loop will be 4 x 3 = 12 times. The below table summarizes the iteration of nested
loops:
Value of i Value of j
0 1
1 1
2 1
3 1
2
Value of i Value of j
Question 5
1. The break statement can be used to terminate inner as well as outer loop.
2. The outer loop can be terminated from the block of inner loop.
3. The inner loop can be terminated from the block of outer loop.
4. The inner loop repeats the execution a number of times for each iteration of the outer loop.
Answer
The inner loop can be terminated from the block of outer loop.
Reason — The inner loop cannot be terminated from the block of outer loop.
Question 1
Question 2
Question 3
The outer loop follows next iteration when iterations of inner loop is over.
True
Question 4
Question 5
Labelled break statement allows the next iteration of the loop from any place of looping structure.
False
Question 6
Answer
When we use a loop within another loop, it is said to be a nested loop. The inner loop repeats a number of times for
each repetition of the outer loop.
Question 2
Answer
executable statement(s)
Question 3
What action will you take to terminate an outer loop from the block of an inner loop?
Answer
An outer loop can be terminated from the block of an inner loop by using labelled break statement as shown in the
example given below:
Question 4
Answer
break outer will terminate the loop that is labelled as outer in a nested loop and transfer the program control to the
statement just after the loop labelled as outer.
continue outer will skip the remaining statements of the nested loop and start the next iteration of the loop that is
labelled as outer.
Question 6
Answer
do {
//statements of outer do-while loop
..
..
do {
//statements of inner do-while loop
} while (<condition>);
..
..
} while (<condition>);
(b) Construct of Nested while loop
while (<condition>) {
//statements of outer while loop
..
..
while (<condition>) {
//statements of inner while loop
}
..
..
}
Solutions to Unsolved Java Programs
Question 1
import java.util.Scanner;
Output
import java.util.Scanner;
}
}
Output
import java.util.Scanner;
}
}
Output
import java.util.Scanner;
}
}
Output
import java.util.Scanner;
if (isPrime) {
sum += j / Math.pow(a, i);
lastPrime = j;
break;
}
}
}
System.out.println("Sum=" + sum);
}
}
Output
Question 2
Write a program to input two numbers and check whether they are twin prime numbers or not.
Hint: Twin prime numbers are the prime numbers whose difference is 2.
For example: (5,7), (11,13), ....... and so on.
import java.util.Scanner;
if (a % i == 0) {
isAPrime = false;
break;
}
}
if (b % i == 0) {
isBPrime = false;
break;
}
if (isBPrime)
System.out.println(a + " and " + b + " are twin prime");
else
System.out.println(a + " and " + b + " are not twin prime");
}
else
System.out.println(a + " and " + b + " are not twin prime");
}
}
Output
Question 3
Write a program to display all the numbers between 100 and 200 which don't contain zeros at any position.
For example: 111, 112, 113, ....... , 199
int count = 0;
for (int i = 100; i <= 200; i++) {
t /= 10;
}
if (isNoZero) {
System.out.print(i + " ");
count++;
}
}
}
Output
Question 4
Write a program to display all prime palindrome numbers between 10 and 1000.
[Hint: A number which is prime as well a palindrome is said to be 'Prime Palindrome' number.]
For example: 11, 101, 131, 151,
int count = 0;
if (revNum == i) {
boolean isPrime = true;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.print(i + " ");
count++;
if (count == 10) {
System.out.println();
count = 0;
}
}
}
}
}
}
Output
Question 5
In an entrance examination, students have been appeared in English, Maths and Science papers. Write a program to
calculate and display average marks obtained by all the students. Take number of students appeared and marks
obtained in all three subjects by every student along with the name as inputs.
Display the name, marks obtained in three subjects and the average of all the students.
import java.util.Scanner;
System.out.println();
Question 6
Write a program in Java to enter a number containing three digits or more. Arrange the digits of the entered number
in ascending order and display the result.
Sample Input: Enter a number 4972
Sample Output: 2, 4, 7, 9
import java.util.Scanner;
public class KboatDigitSort
{
public void sortDigits() {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number having 3 or more digits: ");
int OrgNum = in.nextInt();
System.out.println();
}
}
Output
Question 7
Write a program to input a number and check whether it is 'Magic Number' or not. Display the message accordingly.
A number is said to be a magic number if the eventual sum of digits of the number is one.
Sample Input : 55
Then, 5 + 5 = 10, 1 + 0 = 1
Sample Output: Hence, 55 is a Magic Number.
Similarly, 289 is a Magic Number.
import java.util.Scanner;
while (n > 9) {
int sum = 0;
while (n != 0) {
int d = n % 10;
n /= 10;
sum += d;
}
n = sum;
}
if (n == 1)
System.out.println(num + " is Magic Number");
else
System.out.println(num + " is not Magic Number");
}
}
Output
Question 8
A number is said to be Multiple Harshad number, when divided by the sum of its digits, produces another 'Harshad
Number'. Write a program to input a number and check whether it is a Multiple Harshad Number or not.
(When a number is divisible by the sum of its digit, it is called 'Harshad Number').
import java.util.Scanner;
Output
Question 9
(a)
1
31
531
7531
97531
Output
(b)
1 2 3 4 5
6 7 8 9
10 11 12
13 14
15
(c)
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1
Output
(d)
1
10
101
1010
10101
Output
(e)
55555
4444
333
22
1
System.out.println();
}
}
}
Output
(f)
12345
22345
33345
44445
55555
System.out.println();
}
}
}
Output
(g)
*
*#
*#*
*#*#
*#*#*
(h)
54321
5432
543
54
5
(i)
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Output
Question 10
Write a program to generate a triangle or an inverted triangle till n terms based upon the user's choice.
Example 1:
Input: Type 1 for a triangle and
Type 2 for an inverted triangle
Enter your choice 1
Enter the number of terms 5
Sample Output:
1
22
333
4444
55555
Example 2:
Input: Type 1 for a triangle and
Type 2 for an inverted triangle
Enter your choice 2
Enter the number of terms 6
Sample Output:
666666
55555
4444
333
22
1
import java.util.Scanner;
switch (ch) {
case 1:
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i + " ");
}
System.out.println();
}
break;
case 2:
for (int i = n; i > 0; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(i + " ");
}
System.out.println();
}
break;
default:
System.out.println("Incorrect Choice");
}
}
}
Output
Question 11
Using the switch statement, write a menu driven program for the following:
import java.util.Scanner;
switch (ch) {
case 1:
int a = 1;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(a++ + "\t");
}
System.out.println();
}
break;
case 2:
String s = "ICSE";
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j <= i; j++) {
System.out.print(s.charAt(j) + " ");
}
System.out.println();
}
break;
default:
System.out.println("Incorrect Choice");
}
}
}
Output
Question 12
Using the switch case statement, write a menu driven program for the following:
(a) To input a number and display only those factors of the numbers which are prime.
Sample Input: 84
Sample Output: 2, 3, 7
(b) A program that displays the multiplication table from 1 to 10, as shown:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
...................................
...................................
...................................
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
import java.util.Scanner;
public class KboatPrimeFactorsNTables
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
switch (ch) {
case 1:
System.out.print("Enter a number: ");
int num = in.nextInt();
case 2:
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++)
System.out.print((i * j) + " ");
System.out.println();
}
break;
default:
System.out.println("Incorrect Choice");
}
}
}
Output