Nested for loops

ICSE Class 9 Computer Applications · Solutions for Class 9 ICSE Logix Kips Computer Applications with BlueJ Java · 17 solved questions

Exercise
1Assignment Questions

Write a program to generate the following output.

*
* #
* # *
* # * #
* # * # *
Show SolutionHide Solution↓
public class KboatPattern
{
    public static void main() {
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= i; j++) {
                if (j % 2 == 0)
                    System.out.print("# ");
                else
                    System.out.print("* ");
            }
            System.out.println();
        }
    }
}
BlueJ output of KboatPattern.java
2Assignment Questions

Write a program to print the series given below.

5 55 555 5555 55555 555555

Show SolutionHide Solution↓
public class Kboat5Series
{
    public static void main(String args[]) {
        
        for (int i = 1; i <= 6; i++) {
            for (int j = 0; j < i; j++) {
                System.out.print('5');
            }
            System.out.print(' ');
        }
    }
}
BlueJ output of Kboat5Series.java
3(i)Assignment Questions

Write a program in Java to display the following patterns.

1
2  3
4  5  6
7  8  9  10
11 12 13 14 15
Show SolutionHide Solution↓
public class KboatPattern
{
    public static void main(String args[]) {
        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();
        }
    }
}
BlueJ output of KboatPattern.java
3(ii)Assignment Questions

Write a program in Java to display the following patterns.

1 * * * *
* 2 * * *
* * 3 * *
* * * 4 *
* * * * 5
Show SolutionHide Solution↓
public class KboatPattern
{
    public static void main(String args[])  {
        char ch = '*';
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= 5 ; j++) {
                if(i == j)
                    System.out.print(i + " ");
                else
                    System.out.print(ch + " ");
            }
            System.out.println();
        }
    }
}
BlueJ output of KboatPattern.java
3(iii)Assignment Questions

Write a program in Java to display the following patterns.

#
* *
# # #
* * * * 
# # # # #
Show SolutionHide Solution↓
public class KboatPattern
{
    public static void main(String args[])  {
        char ch1 = '#', ch2 = '*';
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= i; j++) {
                if (i % 2 == 0)
                    System.out.print(ch2 + " ");
                else
                    System.out.print(ch1 + " ");
            }
            System.out.println();
        }
    }
}
BlueJ output of KboatPattern.java
3(iv)Assignment Questions

Write a program in Java to display the following patterns.

1 * * * *
2 2 * * *
3 3 3 * *
4 4 4 4 *
5 5 5 5 5
Show SolutionHide Solution↓
public class KboatPattern
{
    public static void main(String args[]) {

        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i);
            }

            for (int k = 1; k <= 5 - i; k++) {
                System.out.print('*');
            }
            System.out.println();
        }
    }
}
BlueJ output of KboatPattern.java
3(v)Assignment Questions

Write a program in Java to display the following patterns.

* * * * 5
* * * 4
* * 3
* 2
1
Show SolutionHide Solution↓
public class KboatPattern
{
    public static void main(String args[])  {
        char ch = '*';
        for (int i = 5; i >= 1; i--) {
            for (int j = i-1; j >= 1 ; j--) 
                    System.out.print(ch + " ");
            System.out.print(i);
            System.out.println();
        }
    }
}
BlueJ output of KboatPattern.java
3(vi)Assignment Questions

Write a program in Java to display the following patterns.

A B C D E
A B C D
A B C 
A B 
A
Show SolutionHide Solution↓
public class KboatPattern
{
    public static void main(String args[])  {
        for (int i = 69; i >= 65; i--) {
            for (int j = 65; j <= i; j++) {
                System.out.print((char)j);
            }
            System.out.println();
        }
    }
}
BlueJ output of KboatPattern.java
3(vii)Assignment Questions

Write a program in Java to display the following patterns.

5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
Show SolutionHide Solution↓
public class KboatPattern
{
    public static void main(String args[]) {
        for (int i = 5; i >= 1; i--) {
            for (int j = i; j >= 1; j--)
                System.out.print( j + " ");
            System.out.println();
        }

    }
}
BlueJ output of KboatPattern.java
3(viii)Assignment Questions

Write a program in Java to display the following patterns.

J
J A 
J A V
J A V A
Show SolutionHide Solution↓
public class KboatPattern
{
    public static void main(String args[])  {
        String str = "JAVA";
        int len = str.length();
        for(int i = 0; i < len; i++) {
            for(int j = 0; j <= i; j++) {
                System.out.print(str.charAt(j) + " ");
            }
            System.out.println();
        }
    }
}
BlueJ output of KboatPattern.java
3(ix)Assignment Questions

Write a program in Java to display the following patterns.

1 2 3 4 5
1 2 3 4
1 2 3
1 2 
1
1 2 
1 2 3
1 2 3 4
1 2 3 4 5
Show SolutionHide Solution↓
public class KboatPattern
{
    public static void main(String args[])  {
        for(int i = 5; i >= 1; i--) {
            for(int j = 1; j <= i; j++) 
                System.out.print(j + " ");
            System.out.println();
        }
        for(int i = 2; i <= 5; i++) {
            for(int j = 1; j <= i; j++) 
                System.out.print(j + " ");
            System.out.println();
        }
    }
}
BlueJ output of KboatPattern.java
3(x)Assignment Questions

Write a program in Java to display the following patterns.

1 2 3 4 5
  2 3 4 5 
    3 4 5
      4 5
        5
      4 5
    3 4 5
  2 3 4 5 
1 2 3 4 5
Show SolutionHide Solution↓
public class KboatPattern
{
    public static void main(String args[])  {
        
        for(int i = 1; i <= 5; i++) {
            for (int j = 1; j < i; j++) {
                System.out.print("  ");
            }
            for (int k = i; k <= 5; k++) {
                System.out.print(k + " ");
            }
            System.out.println();                  
        }
        for(int i = 4; i >= 1 ; i--) {
            for (int j = 1; j < i; j++) {
                System.out.print("  ");
            }
            for (int k = i; k <= 5; k++) {
                System.out.print(k + " ");
            }
            System.out.println();                  
        }
    }
}
BlueJ output of KboatPattern.java
4(i)Assignment Questions

Distinguish between the following:

break statement and labelled break statement

Show SolutionHide Solution↓
break statementlabelled break statement
break statement is used to take the control out of the loop control structure immediately enclosing it.Labelled break statement transfers program control out of the code block whose label is specified as its target.
Syntax:
break;
Syntax:
break <label>;
For example,
for (int i = 1; i <= 3; i++)
{
for(int j = 1; j <= 2; j++)
{
if (i == 2)
{
break;
}
System.out.println("Iteration value of inner loop: " + j);
}
System.out.println();
}
For example,
outer:
for (int i = 1; i <= 3; i++)
{
for(int j = 1; j <= 2; j++)
{
if (i == 2)
{
System.out.println("Terminating the Outer-loop");
break outer;
}
System.out.println("i =" + i +" ; j =" + j);
}
4(ii)Assignment Questions

Distinguish between the following:

continue statement and labelled continue statement

Show SolutionHide Solution↓
continue statementlabelled continue statement
continue statement is used to skip the current iteration of a loop and move on to the next iteration.A labelled continue statement skips the current iteration of an outer loop by using a label before the loop and including the same label in continue statement.
Syntax:
continue;
Syntax:
continue <label>;
For example,
for (int i = 0; i <= 3; i++)
{
System.out.println("i =" + i);
for(int j = 0; j < 3; j++)
{
if (i == 2)
{
continue;
}
System.out.print("j = " + j + " ");
}
System.out.println();
}
For example,
outerLoop:
for (int i = 0; i <= 3; i++)
{
for(int j = 0; j < 3; j++)
{
if (i == 2)
{
continue outerLoop;
System.out.println("i =" + i +" ; j =" + j);
}
}
}
5(i)Assignment Questions

What will be the value of sum after each of the following nested loops is executed?

int sum = 0;
for (int i = 0; i <= 10; i++)
    for (int j = 0; j <= 10; j++) 
        sum += i;
Show SolutionHide Solution↓

Sum = 605

The outer loop executes 11 times. For each iteration of outer loop, the inner loop executes 11 times. For every value of j, i is added to sum 11 times. Consider the following table for the value of sum with each value of i and j.

ijSumRemarks
00 - 1000 is added to sum 11 times
10 - 10111 is added to sum 11 times
⇒ 0 + 11 = 11
20 - 10332 is added to sum 11 times
⇒ 11 + 22 = 33
30 - 10663 is added to sum 11 times
⇒ 33 + 33 = 66
40 - 101104 is added to sum 11 times
⇒ 66 + 44 = 110
50 - 101655 is added to sum 11 times
⇒ 110 + 55 = 165
60 - 102316 is added to sum 11 times
⇒ 165 + 66 = 231
70 - 103087 is added to sum 11 times
⇒ 231 + 77 = 308
80 - 103968 is added to sum 11 times
⇒ 308 + 88 = 396
90 - 104959 is added to sum 11 times
⇒ 396 + 99 = 495
100 - 1060510 is added to sum 11 times
⇒ 495 + 110 = 605
11  Loop terminates
5(ii)Assignment Questions

What will be the value of sum after each of the following nested loops is executed?

int sum = 0;
for (int i = 1; i <= 3; i++) 
    for (int j = 1; j <= 3; j++)
        sum = sum + (i + j);
Show SolutionHide Solution↓

Sum = 36

The outer loop executes 3 times. For each iteration of outer loop, the inner loop executes 3 times. For every value of j, i + j is added to sum 3 times. Consider the following table for the value of sum with each value of i and j.

ijSumsum + (i + j)
1120 + 1 + 1
 252 + 1 + 2
 395 + 1 + 3
21129 + 2 + 1
 21612 + 2 + 2
 32116 + 2 + 3
312521 + 3 + 1
 23025 + 3 + 2
 33630 + 3 + 3
4  Loop terminates
6Assignment Questions

Write a program to generate a triangle or an inverted triangle till n terms based upon the user's choice of triangle to be displayed.

Example 1
Input:
Type 1 for a triangle and type 2 for an inverted triangle
1
Enter the number of terms
5
Output:

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Example 2
Input:
Type 1 for a triangle and type 2 for an inverted triangle
2
Enter the number of terms
6
Output:

6 6 6 6 6 6
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
Show SolutionHide Solution↓
import java.util.Scanner;

public class KboatPattern
{
    public static void main(String args[]) {
        
        Scanner in = new Scanner(System.in);
        System.out.println("Type 1 for a triangle");
        System.out.println("Type 2 for an inverted triangle");
        
        System.out.print("Enter your choice: ");
        int ch = in.nextInt();
        
        System.out.print("Enter the number of terms: ");
        int n = in.nextInt();
        
        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");
        }
    }
}
BlueJ output of KboatPattern.java BlueJ output of KboatPattern.java

Press ↓ to reveal a solution and move to the next question. Back to Solutions for Class 9 ICSE Logix Kips Computer Applications with BlueJ Java.