Chapter 9
Chapter 9
Chapter 9
******************************************************************************
Nested for loops – This topic has been removed from the current 2021 syllabus
5. _______________statement will repeat a loop for next iteration after ignoring some statements
of the loop.
Ans.
1. nested loop
2. break
4. inner , outer
5. continue
4. When the outer loop completes its iterations, the inner loop starts.
5. Labelled continue statement allows the next iteration of the loop from any place of looping
structure.
Ans.
1. true
2. false
3. false
4. false
Nested if
Nested loop
i) A ‘Nested loop’ construct gets executed repeatedly. All the iterations of the inner loop are
executed for each iteration of the outer loop.
break:
i) The break statement can be used in a loop statement as well as in a switch case block.
ii) break statement is used to terminate a block in which it exists. i.e. (In a switch case block, it
completely comes out of switch-case block and in case of loops, it terminates from the particular
loop in which it is present.)
iii)
Example
{
if (i= =5)
break;
System.out.println(i);
The above snippet prints the numbers from 1 to 4. When i=5, the break statement is executed and
hence the control comes out of the loop.
continue:
i) The continue statement can be in a loop statement but not in switch case block.
ii) A continue statement is used to skip the execution of the remaining statements in the current
iteration and moves on with the next iteration.
iii)
Example
if (i= =5)
continue;
System.out.println(i);
}
The above snippet prints all the numbers from 1 to 10 except 5 because of the continue statement.
Ans. The presence of one or more loops or iteration statements within one loop is known as a
nested loop. In other words, a nested loop is defined as a loop programmed within another loop. In
a nested loop, all the iterations of the inner loop get executed for each iteration of the outer loop.
[ A nested loop is somewhat similar to a clock where the second hand moves 60 steps for each step
of the minute hand. So we can consider the minute hand as the outer loop and the second hand as
the inner loop]
{ executable statements
executable statements
}
executable statements
Example:
System.out.print( j + “\t”);
The inner loop prints 1 2 3 for each iteration of the outer loop. Since the outer loop has two
iterations, the inner loop is repeated twice. Therefore, the output of the above snippet is as follows:
1 2 3
1 2 3
Ans.
Nested loops are used mainly used when certain a certain loop or loops need to be repeated again
and again.
Example
Suppose we want to print the numbers from 1 to 7. To avoid lengthy coding, we would prefer to use
a loop for this task and the loop would create an output as shown below:
1234567
Now, suppose we need to repeat this task of printing 1 to 7, four times (each time starting in a new
line), then we would use an outer loop which runs 4 times and an inner loop which runs 7 times.
Thus, for each iteration of the outer loop, the inner loop completes all the iterations. Therefore, the
output will be as follows:
1234567
1234567
1234567
1234567
3. How will you terminate outer loop from the block of the inner loop?
Ans. [To terminate an outer loop from the inner loop, labelled break must be used which is out of
scope of the present syllabus]
A break statement (without a label) given in the inner loop will only terminate the inner loop. In
order to terminate the outer loop, a break statement should be given in the outer loop directly.
Ans.
{ executable statements
executable statements
executable statements
1.
int i,j;
for(i=0;i<4;i++)
for(j=i;j>=0;j–)
System.out.print(j);
System.out.println( );
Ans.
Inner loop : The inner loop runs for j=1 and then j=0
Inner loop : The inner loop runs for j=2 followed by j=1 and then j=0
Inner loop : The inner loop runs for j=3, followed by j=2, followed by j=1 and then j=0
10
210
3210
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( );
Ans.
Inner loop : The inner loop runs for y=1 and then y=2
p=x*y=1* 1=1
p=x*y =1*2 =2
Since ‘p’ values are printed in the same line, the output is 12
Inner loop : The inner loop runs for y=1 and then y=2
p=x*y=2* 1=2
p=x*y =2*2 =4
Since ‘p’ values are printed in the same line, the output is 24
Inner loop : The inner loop runs for y=1 and then y=2
p=x*y=3* 1=3
p=x*y =3*2 =6
Since ‘p’ values are printed in the same line, the output is 36
12
24
36
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( );
Inner loop value : b=64+a =64+1=65, So the inner loop runs from b=65 to b=70
Inner loop value : b=64+a =64+2=66, So the inner loop runs from b=66 to b=70
ABCDEF
BCDEF
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( );
}
Ans.
Condition y<x that is , 1<2 is true so 1 is printed. The output in the first line is 1.
Condition y<x is true for all the y values given above. So, there’s no output in the fourth line and
instead the inner loop is terminated because of the break statement given in the inner loop which is
executed when x=4.
Condition y<x is true for above ‘y’ values. So, the output in the fifth line is, 1234.
12
1234
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( );
Ans. [The above snippet involves a labelled break and hence out of scope of the present syllabus]
Write a program to display the Mathematical Table from 5 to 10 for 10 iterations in the given
format:
5*1=5
5*2=10
……..
……..
5*10=50
Solution:
class Q1 {
for(int i=5;i<=10;i++)
for(int j=1;j<=10;j++)
}}
Write a program to accept any 20 numbers and display only those numbers which are prime.
Solution:
import java.util.*;
class Q2
int n,cnt=0;
for(int x=1;x<=20;x++)
{n=ob.nextInt();
cnt=0;
for(int i=1;i<=n/2;i++)
{if(n%i==0)
cnt++;
if(cnt==1)
s=s+”\n”+n;
} //End of Outer Loop
System.out.println(s);
}}
Write a program to compute and display the sum of the following series:
[ICSE 2007]
Solution:
import java.util.*;
class Q3
int n;
n=ob.nextInt();
for(int i=1;i<=n;i++)
{for(double j=1.0;j<=i+1;j++)
{a+=j;
b*=j;
term=a/b;
sum+=term;
//reset a and b
a=0;
b=1;
}}
Write two separate programs to generate the following patterns using iteration (loop) statements:
a) b)
* 5 4 3 2 1
*# 5 4 3 2
*#* 5 4 3
*#*# 5 4
*#*#* 5
Solution:
class Q4 A{
char s=’*’;
for(int i=1;i<=5;i++)
{for(int j=1;j<=i;j++)
{if(j%2!=0)
s=’*’;
else
s=’#’;
System.out.print(s+” “);
System.out.println();
}}
class Q4 B{
{for(int i=1;i<=5;i++)
{for(int j=5;j>=i;j–)
System.out.print(j+” “);
System.out.println();
}}
Write a program to calculate and display the factorials of all the numbers between ‘m’ and ‘n’
(where m<n, m>0 n>0)
import java.util.*;
class Q5 {
int m,n,fact;
m=ob.nextInt();
n=ob.nextInt();
for(int i=m;i<=n;i++)
for(int j=1;j<=i;j++)
fact=fact*j;
System.out.println(i+”! =”+fact);
}}
}}
Write a menu driven program to display all prime and non-prime numbers from 1 to 100.
Hint: A number is said to be prime if it is only divisible by 1 and the number itself.
Solution:
import java.util.*;
class Q6 {
for(num=1;num<=100;num++)
{if(num%j==0)
cnt++;
if(cnt==2)
s1+=”\n”+num;
else if(cnt>2)
s2+=”\n”+num;
System.out.println(“***MENU***”);
switch(ob.nextInt())
case 1:System.out.println(s1);
break;
case 2:System.out.println(s2);
break;
default:System.out.println(“Invalid input”);
}}
In an entrance examination, students have answered Maths, English and Science papers. Write a
program to calculate and display average marks obtained by all the students. Take the number of
students appeared and marks obtained in all three subjects by every student along with the name as
inputs.
Solution:
import java.util.*;
class Q7 {
static void main()
int n;
double eng=0,math=0,sci=0,avg;
String name;
n=sn.nextInt();
while(n>0)
name=sn.next();
eng=sn.nextInt();
math=sn.nextInt();
System.out.println(“Enter the Science mark”);
sci=sn.nextInt();
avg=(eng+math+sci)/3.0;
n–;
}}
If the number as well as the reverse is ‘Prime’ then display ‘Twisted Prime’ otherwise ‘Not a Twisted
Prime’.
Sample Input:367
It is a ‘Twisted Prime’.
Solution:
import java.util.*;
class Q8
System.out.println(“Enter a number”);
int n,n1,cnt=0,d,rev=0,cnt1=0,cnt2=0;
n=sn.nextInt();
n1=n;
while(n>0)
d=n%10;
rev=rev*10+d;
n=n/10;
}
System.out.println(“Reverse=”+rev);
for(int i=1;i<=n1/2;i++)
{if(n1%i==0)
cnt1++;
for(int i=1;i<=rev/2;i++)
{if(rev%i==0)
cnt2++;
else
System.out.println(“It is not a Twisted Prime number”);
}}
Solution:
class Q9a {
{int fact;
double sum=0;
for(int p=0;p<=n;p++)
for(int j=1;j<=p+1;j++)
fact=fact*j;
sum+=1.0/fact;
System.out.println(“sum :”+sum);
}}
Solution:
import java.util.*;
class Q9b
double sum=0,term=0;
int n;
n=ob.nextInt();
for(int i=1;i<=n;i++)
{term=0;
for(double j=1.0;j<=i;j++)
{ term+=j;
}
sum+=term;
}}
Solution:
import java.util.*;
class Q9c
int n;
double sum=0,term;
n=ob.nextInt();
for(int i=1;i<=n;i++)
{term=1; //reset term=1 since it is multiplication
for(double j=1.0;j<=i;j++)
{ term*=j;
sum+=term;
}}
Solution:
import java.util.*;
class Q9d
int n;
double sum=0,term;
n=ob.nextInt();
for(int i=1;i<=n;i++)
{term=0;
for(double j=1.0;j<=i;j++)
{ term+=j;
sum+=(1.0/term);
}}
Solution:
class Q9e
double term,sum=0;
for(int i=2;i<=4;i++)
term=0.0;
for(int j=1;j<=i/2;j++)
{if(i%j==0)
cnt++;
if(cnt==1)
{term=1.0/i;
sum+=term;
}}
System.out.println(“Sum of the series=”+sum);
}}
a) 1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
Solution:
class Q10a {
for(int i=1;i<=5;i++)
{for(int j=i;j>=1;j–)
System.out.print(j +” “);
System.out.println();
}
}}
b) 1 2 3 4 5
1 2 3 4
1 2 3
1 2
class Q10b {
for(int i=5;i>=1;i–)
{for(int j=1;j<=i;j++)
System.out.print(j +” “);
System.out.println();
}}
c) 5 4 3 2 1
5 4 3 2
5 4 3
5 4
Solution:
class Q10c {
for(int i=1;i<=5;i++)
{for(int j=5;j>=i;j–)
System.out.print(j+” “);
System.out.println();
}}
d) 1 3 5 7 9
1357
135
13
Solution:
class Q10d {
for(int i=9;i>=1;i=i-2)
{for(int j=1;j<=i;j=j+2)
System.out.print(j +” “);
System.out.println();
}}
e) 5
54
5 4 3
5 4 3 2
5 4 3 2 1
Solution:
class Q10e {
for(int i=5;i>=1;i–)
{for(int j=5;j>=i;j–)
System.out.print(j +” “);
System.out.println();
}}
f) 1 2 3 4 5
2345
345
45
Solution:
class Q10f {
for(int i=9;i>=1;i=i-2)
{for(int j=i;j<=9;j=j+2)
System.out.print(j +” “);
System.out.println();
}}
g) 9 9 9 9 9
77777
55555
33333
11111
Solution:
class Q10g {
static void main(){
int p=1;
for(int i=9;i>=1;i=i-2)
{for(int j=1;j<=5;j++)
System.out.print(i);
System.out.println();
}}
h) 9
79
579
3579
13579
Solution:
class Q10h {
{for(int j=i;j<=9;j=j+2)
System.out.print(j +” “);
System.out.println();
}}
i) 9
97
975
9753
97531
Solution:
class Q10i {
for(int i=9;i>=1;i=i-2)
{for(int j=9;j>=i;j=j-2)
System.out.print(j +” “);
System.out.println();
}}
j)
2 3
4 5 6
7 8 9 10
11 12 13 14 15 [ICSE 2016]
Solution:
class Q10j {
int p=1;
for(int i=1;i<=5;i++)
{for(int j=1;j<=i;j++)
System.out.print(p++ +” “);
System.out.println();
}}
*************