KnowledgeBoat Logo
OPEN IN APP

Chapter 10

Nested for loops

Class 10 - Logix Kips ICSE Computer Applications with BlueJ



Assignment Questions

Question 1

Write a program to generate the following output.

@
@ #
@ # @
@ # @ #
@ # @ # @
public class KboatPattern
{
    public static void main(String args[]) {
        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();
        }
    }
}
Output
BlueJ output of KboatPattern.java

Question 2(i)

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
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();
        }
    }
}
Output
BlueJ output of KboatPattern.java

Question 2(ii)

Write a program in Java to display the following patterns.

1 * * * *
* 2 * * *
* * 3 * *
* * * 4 *
* * * * 5
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();
        }
    }
}
Output
BlueJ output of KboatPattern.java

Question 2(iii)

Write a program in Java to display the following patterns.

#
* *
# # #
* * * * 
# # # # #
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();
        }
    }
}
Output
BlueJ output of KboatPattern.java

Question 2(iv)

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
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();
        }
    }
}
Output
BlueJ output of KboatPattern.java

Question 2(v)

Write a program in Java to display the following patterns.

$ $ $ $ 5
$ $ $ 4
$ $ 3
$ 2
1
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();
        }
    }
}
Output
ICSE Computer Applications Java Pattern Program

Question 2(vi)

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
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();
        }
    }
}
Output
BlueJ output of KboatPattern.java

Question 2(vii)

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
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();
        }

    }
}
Output
BlueJ output of KboatPattern.java

Question 2(viii)

Write a program in Java to display the following patterns.

I
I C
I C S
I C S E
public class KboatPattern
{
    public static void main(String args[])  {
        String str = "ICSE";
        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();
        }
    }
}
Output
BlueJ output of KboatPattern.java

Question 2(ix)

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
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();
        }
    }
}
Output
BlueJ output of KboatPattern.java

Question 2(x)

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
public class KboatPattern
{
    public static void main(String args[]) {
        
        for (int i = 1; i <= 5; i++)
        {
            for (int j = i; j <= 5; j++) {
                System.out.print(j);
            }
            System.out.println();
        }
        
        for (int k = 1; k <= 4; k++) {
            for (int l = 5 - k; l <= 5; l++) {
                System.out.print(l);
            }
            System.out.println();
        }
        
    }
}
Output
BlueJ output of KboatPattern.java

Question 3(i)

Distinguish between the following:

break statement and labelled break statement

Answer

break statementlabelled break statement
The 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);
}

Question 3(ii)

Distinguish between the following:

continue statement and labelled continue statement

Answer

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);
}
}
}

Question 4

Write a program to print the series given below.

8 88 888 8888 88888 888888

public class Kboat8Series
{
    public static void main(String args[]) {
        
        for (int i = 1; i <= 6; i++) {
            for (int j = 0; j < i; j++) {
                System.out.print('8');
            }
            System.out.print(' ');
        }
    }
}
Output
BlueJ output of Kboat8Series.java

Question 5(i)

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 ;

Answer

Sum = 605

Explanation

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

Question 5(ii)

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);

Answer

Sum = 36

Explanation

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

Question 6

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
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");
        }
    }
}
Output
BlueJ output of KboatPattern.java
BlueJ output of KboatPattern.java

Question 7

Write a program to compute and display factorials of numbers between p and q where p > 0, q > 0, and p > q.

import java.util.Scanner;

public class KboatFactRange
{
    public static void main(String args[]) {

        Scanner in = new Scanner(System.in);
        System.out.print("Enter p: ");
        int p = in.nextInt();
        System.out.print("Enter q: ");
        int q = in.nextInt();

        if (p > q && p > 0 && q > 0) {
            for (int i = q; i <= p; i++) {
                long fact = 1;
                for (int j = 1; j <= i; j++)
                    fact *= j;
                System.out.println("Factorial of " + i + 
                                        " = " + fact);
            }
        }
        else {
            System.out.println("Invalid Input");
        }

    }
}
Output
BlueJ output of KboatFactRange.java

Question 8

Write a program to determine if an entered number is a Happy Number. A happy number is defined by the following process:

Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number is equal to 1.
For example, 19 is a happy number, as per the following calculation:
12 + 92 = 82,
82 + 22 = 68,
62 + 82 = 100,
12 + 02 + 02 = 1

import java.util.Scanner;

public class KboatHappyNumber
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter number to check: ");
        long num = in.nextLong();
        long sum = 0;
        long n = num;
        do {
            sum = 0;
            while (n != 0) {
                int d = (int)(n % 10);
                sum += d * d;
                n /= 10;
            }
            n = sum;
        } while (sum > 6);

        if (sum == 1) {
            System.out.println(num + " is a Happy Number");
        }
        else {
            System.out.println(num + " is not a Happy Number");
        }
    }
}
Output
BlueJ output of KboatHappyNumber.java
BlueJ output of KboatHappyNumber.java

Question 9

Write a menu driven program that prompts the user to select one of the four triangle patterns (a, b, c, or d). The program then accepts number of rows and prints the selected pattern as shown below:

Enter the size : 8

(a)

#
# #
# # #
# # # #
# # # # #
# # # # # #
# # # # # # #
# # # # # # # #

(b)

# # # # # # # #
# # # # # # # 
# # # # # # 
# # # # # 
# # # # 
# # # 
# # 
#

(c)

# # # # # # # #
  # # # # # # # 
    # # # # # # 
      # # # # # 
        # # # # 
          # # # 
            # # 
              #

(d)

              #
            # #
          # # #
        # # # #
      # # # # #
    # # # # # #
  # # # # # # # 
# # # # # # # #
import java.util.Scanner;

public class KboatTriangleChoice
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Type a for Pattern a");
        System.out.println("Type b for Pattern b");
        System.out.println("Type c for Pattern c");
        System.out.println("Type d for Pattern d");
        
        System.out.print("Enter your choice: ");
        char ch = in.next().charAt(0);
        
        System.out.print("Enter the number of terms: ");
        int n = in.nextInt();
        
        switch (ch) {
            case 'a':
                for (int i = 1; i <= n; i++) { 
                    for (int j = 1; j <= i; j++) 
                        System.out.print('#');
                    
                    System.out.println();
                }
                break;
            
            case 'b': 
                for (int i = n; i >= 1; i--) {   
                    for (int j = 1; j <= i; j++) 
                        System.out.print('#');
                    
                    System.out.println();
                }
                break;
            
            case 'c': 
                for (int i = n; i >= 1; i--) {
                    for (int j = i; j < n; j++) 
                        System.out.print(" ");
                    for (int k = 0; k < i; k++) 
                        System.out.print('#');
                    System.out.println();
                }
                break;
                
            case 'd': 
                for (int i = n; i >= 1; i--) {
                    for (int j = 1; j < i; j++)
                        System.out.print(" ");
                    for (int k = i; k <= n; k++)
                        System.out.print('#');
                    System.out.println();
                }
                break;
            default:
                System.out.println("Incorrect choice");
                break;
        }
    }
}
Output
BlueJ output of KboatTriangleChoice.java
BlueJ output of KboatTriangleChoice.java
BlueJ output of KboatTriangleChoice.java
BlueJ output of KboatTriangleChoice.java

Question 10

Write a menu driven program that prompts the user to select one of the four triangle patterns (a, b, c or d). The program then accepts number of rows and prints the selected pattern as shown below:

Enter the size: 8

(a)

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6 
1 2 3 4 5 6 7 
1 2 3 4 5 6 7 8

(b)

1 2 3 4 5 6 7 8
  1 2 3 4 5 6 7 
    1 2 3 4 5 6 
      1 2 3 4 5 
        1 2 3 4 
          1 2 3 
            1 2 
              1

(c)

              1
            1 2
          1 2 3
        1 2 3 4 
      1 2 3 4 5 
    1 2 3 4 5 6
  1 2 3 4 5 6 7
1 2 3 4 5 6 7 8

(d)

1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 
1 2 3 4 5 6 
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1
import java.util.Scanner;

public class KboatTriangleChoice
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Type a for Pattern a");
        System.out.println("Type b for Pattern b");
        System.out.println("Type c for Pattern c");
        System.out.println("Type d for Pattern d");
        
        System.out.print("Enter your choice: ");
        char ch = in.next().charAt(0);
        
        System.out.print("Enter the number of terms: ");
        int n = in.nextInt();
        
        switch (ch) {
            case 'a':
                for (int i = 1; i <= n; i++) { 
                    for (int j = 1; j <= i; j++) 
                        System.out.print(j);
                    System.out.println();
                }
                break;
            
            case 'b': 
                for (int i = n; i >= 1; i--)    {   
                    for (int j = i; j < n; j++) 
                        System.out.print(" ");    
                    for (int k = 1; k <= i; k++)
                        System.out.print(k);    
                    System.out.println();
                }
                break;
            
            case 'c': 
                for (int i = 1; i <= n; i++) {
                    for (int j = i; j < n; j++)
                        System.out.print(" ");
                    for (int k = 1; k <= i; k++)
                        System.out.print(k);
                    
                    System.out.println();
                }
                break;
                
            case 'd': 
                for (int i = n; i >= 1; i--) {
                    for (int j = 1; j <= i; j++) 
                        System.out.print(j);
                    System.out.println();
                }
                break;
            default:
                System.out.println("Incorrect choice");
                break;
        }
    }
}
Output
BlueJ output of KboatTriangleChoice.java
BlueJ output of KboatTriangleChoice.java
BlueJ output of KboatTriangleChoice.java
BlueJ output of KboatTriangleChoice.java

Question 11

Write a program that computes sin x and cos x by using the following power series:

sin x = x - x3/3! + x5/5! - x7/7! + ......

cos x = 1 - x2/2! + x4/4! - x6/6! + ......

import java.util.Scanner;

public class KboatSeries
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter x: ");
        int x = in.nextInt();
        
        double sinx = 0, cosx = 0;
        int a = 1;
        
        for (int i = 1; i <= 20; i += 2) {
            long f = 1;
            for (int j = 1; j <= i; j++) 
                f *= j;
            
            double t = Math.pow(x, i) / f * a;
            sinx += t;
            a *= -1;
        }
        
        a = 1;
        for (int i = 0; i <= 20; i += 2) {
            long f = 1;
            for (int j = 1; j <= i; j++) 
                f *= j;
            
            double t = Math.pow(x, i) / f * a;
            cosx += t;
            a *= -1;
        }
        
        System.out.println("Sin x = " + sinx);
        System.out.println("Cos x = " + cosx);
    }
}
Output
BlueJ output of KboatSeries.java

Video Explanations

PrevNext