Home / Programs / Write two separate programs to generate the following patterns using iteration (loop) statements: (a) **  #*  #  **  #  *  #*  #  *  #  * (b) 5 4 3 2 15 4 3 25 4 35 45
Programming Example

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

👁 111 Views
💻 Practical Program
📘 Step by Step Learning
Study this program carefully to understand the logic, output, and explanation in a structured way.

Information & Algorithm

Given Input:


Expected Output:


Program Code

public class RAnsariPattern
{
    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();
        }
    }
}





public class RAnsariPattern
{
    public static void main(String args[]) {
        for (int i = 1; i <=5; i++) {
            for (int j = 5; j >= i; j--) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }
}

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.