Std 9 Chapter 9 Nested for loops
Std 9 Chapter 9 Nested for loops
Question 1
Question 2
Question 3
Question 4
Question 5
Continue statement will repeat a loop for next iteration after ignoring some statements of the
loop.
Question 1
Question 2
Question 4
When outer loop completes its iterations, the inner loop starts. False
Question 5
Labelled continue statement allows the next iteration of the loop from any place of looping
structure.
False
Question 1
Question 2
1. break statement is used to unconditionally jump out of the loop whereas continue
statement is used to unconditionally jump to the next iteration of the loop, skipping
the remaining statements of the current iteration.
2. break statement is used in switch-case and loops whereas continue statement is only
used in loops.
Question 3
Question 1
What do you mean by a nested loop?
When a loop is contained inside another loop it is termed as nested loops
Question 2
Question 3
How will you terminate outer loop from the block of the inner loop?
By using Labelled break statement.
Question 4
Question 5
Question 1
int i,j;
for (i=0; i<4; i++)
{
for (j=i; j>=0; j--)
System.out.print(j);
System.out.println();
}
Output
10
210
3210
Explanation
For each iteration of outer for loop, inner for loop will iterate from i to 0 printing the above
pattern.
Question 2
int y,p;
for (int x=1; x<=3; x++)
{
for (y=1; y<=2; y++)
{
p = x * y;
System.out.print(p);
}
System.out.println( );
}
Output
12
24
36
Explanation
x y p Remarks
2 2
2 4
2 6
Question 3
int a,b;
for (a=1; a<=2; a++)
{
for (b= (64+a); b<=70; b++)
System.out.print((char) b);
System.out.println( );
}
Output
ABCDEF
BCDEF
Explanation
In the first iteration of outer for loop, the inner for loop will print characters with ASCII codes
from 65 to 70 i.e. letters from A to F.
In the second iteration of outer for loop, the inner for loop will print characters with ASCII codes
from 66 to 70 i.e. letters from B to F.
Question 4
int x,y;
for(x=1; x<=5; x++)
{
for(y=1; y<x; y++)
{
if(x == 4)
break;
System.out.print(y);
}
System.out.println( );
}
Output
12
1234
Explanation
Question 5
int i,j;
first:
for (i=10; i>=5; i--)
{
for (j= 5; j<=i; j++)
{
if (i*j <40)
continue first;
System.out.print(j);
}
System.out.println( );
}
Output
5678910
56789
5678
Explanation
For the first 3 iterations of outer loop i * j >= 40. After that as the condition of if (i*j
<40) becomes true, in each iteration of inner for, continue statement transfers the program
control to the next iteration of outer for loop.
Solutions to Unsolved Java Programs
Question 1
Write a program to display the Mathematical Table from 5 to 10 for 10 iterations in the given
format:
Sample Output: Table of 5
5*1 = 5
5*2 =10
--------
--------
5*10 = 50
Output
Question 2
Write a program to accept any 20 numbers and display only those numbers which are prime.
Hint: A number is said to be prime if it is only divisible by 1 and the number itself.
import java.util.Scanner;
Output
Question 3
Write a program to compute and display the sum of the following series:
S = (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + -------- + (1 + 2 + 3 + ----- + n ) / (1 * 2 * 3 *
----- * n)
import java.util.Scanner;
Output
Question 4
Write two separate programs to generate the following patterns using iteration (loop) statements:
(a)
*
* #
* # *
* # * #
* # * # *
(b)
54321
5432
543
54
5
Output
Question 5
Write a program to calculate and display the factorials of all the numbers between 'm' and 'n'
(where m<n, m>0, n>0).
[Hint: factorial of 5 means: 5!=5*4*3*2*1]
import java.util.Scanner;
}
}
Output
Question 6
Write a menu driven program to display all prime and non-prime numbers from 1 to 100.
Enter 1: to display all prime numbers
Enter 2: to display all non-prime numbers
Hint: A number is said to be prime if it is only divisible by 1 and the number itself.
import java.util.Scanner;
switch (choice) {
case 1:
for (int i = 2; i <= 100; i++) {
boolean isPrime = true;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime)
System.out.println(i);
}
break;
case 2:
System.out.println(1);
for (int i = 2; i <= 100; i++) {
boolean isPrime = true;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (!isPrime)
System.out.println(i);
}
break;
default:
System.out.println("Incorrect Choice");
break;
}
}
}
Output
Question 7
In an entrance examination, students have answered 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.
import java.util.Scanner;
Output
Question 8
import java.util.Scanner;
if (num == 1) {
System.out.println(num + " is not a twisted prime
number");
}
else {
boolean isPrime = true;
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
int t = num;
int revNum = 0;
while (t != 0) {
int digit = t % 10;
t /= 10;
revNum = revNum * 10 + digit;
}
if (isPrime)
System.out.println(num + " is a twisted
prime number");
else
System.out.println(num + " is not a twisted
prime number");
}
}
}
Output
Question 9
import java.util.Scanner;
Output
import java.util.Scanner;
import java.util.Scanner;
Output
(d) 1 + 1 / (1+2) + 1 / (1+2+3) + .......... + 1 / (1+2+3+.....+n)
import java.util.Scanner;
Output
(e) (1/2) + (1/3) + (1/5) + (1/7) + (1/11) + .......... + (1/29)
Output
Question 10
(a)
1
21
321
4321
54321
Output
(b)
12345
1234
123
12
1
Output
(c)
54321
5432
543
54
5
Output
(d)
13579
1357
135
13
1
Output
(e)
5
54
543
5432
54321
Output
(f)
12345
2345
345
45
5
Output
(g)
99999
77777
55555
33333
11111
Output
(h)
9
79
579
3579
13579
Output
(i)
9
97
975
9753
97531
Output
(j)
1
23
456
7 8 9 10
11 12 13 14 15
Output