You are on page 1of 8

Java

Q1\

public class q1pattern {

public static void main(String[] args) {

int n = 5;

// Outer loop for the number of rows

for (int i = 1; i <= n; i++) {

// Inner loop for printing asterisks in each row

for (int j = 1; j <= i; j++) {

System.out.print("* ");

// Move to the next line after printing asterisks in a row

System.out.println("");

Explanation:

1. Outer Loop (for (int i = 1; i <= n; i++)): This loop is responsible for iterating
through each row of the pyramid. It starts with the first row and goes up to
the specified number of rows ( n).
2. Inner Loop (for (int j = 1; j <= i; j++)): This loop is nested inside the outer loop. It
is responsible for printing the asterisks in each row. The number of asterisks to
be printed in a row is determined by the value of the outer loop variable i. In
the first row, i is 1, so it prints one asterisk. In the second row, i is 2, so it prints
two asterisks, and so on.
3. Printing Asterisks (System.out.print("* ");): Inside the inner loop, it prints an
asterisk followed by a space. This creates the pattern of asterisks separated by
spaces in each row.
4. Move to the Next Line (System.out.println("");): After printing the asterisks in a
row, it moves to the next line to start a new row in the pyramid.
Overall, the program uses nested loops to control the number of rows and the
number of asterisks in each row, resulting in a left half pyramid pattern when
executed. For n = 5, the output would be:

Q2 public class q2patternjava {

public static void main(String[] args) {

int n = 6;

// Outer loop for the number of rows

for (int i = 1; i <= n; i++) {

// Inner loop for printing asterisks in each row

for (int j = i; j <= n; j++) {

System.out.print("* ");

// Move to the next line after printing asterisks in a row

System.out.println("");

Explanation:

1. Outer Loop (for (int i = 1; i <= n; i++)): This loop is responsible for iterating
through each row of the right-angled triangle. It starts with the first row and
goes up to the specified number of rows ( n).
2. Inner Loop (for (int j = i; j <= n; j++)): This loop is nested inside the outer loop.
It is responsible for printing the asterisks in each row. The number of asterisks
to be printed in a row is determined by the value of the outer loop variable i.
In the first row, i is 1, so it prints asterisks from 1 to n. In the second row, i is 2,
so it prints asterisks from 2 to n, and so on.
3. Printing Asterisks (System.out.print("* ");): Inside the inner loop, it prints an
asterisk followed by a space. This creates the pattern of asterisks in each row.
4. Move to the Next Line (System.out.println("");): After printing the asterisks in a
row, it moves to the next line to start a new row in the right-angled triangle.
Q3 The provided Java program prints a pattern of asterisks in the form of an inverted right-angled
triangle. Let's break down the logic and algorithm:

```java

public class q3pattern {

public static void main(String[] args) {

int n = 5;

// Outer loop for the number of rows

for (int i = 1; i <= n; i++) {

// Inner loop to print spaces before asterisks

for (int j = i; j <= n; j++) {

System.out.print(" ");

// Inner loop for printing asterisks in each row

for (int j = 1; j <= i; j++) {

System.out.print("*");

// Move to the next line after printing spaces and asterisks in a row

System.out.println(" ");

```

Explanation:

1. **Outer Loop (`for (int i = 1; i <= n; i++)`):** This loop is responsible for iterating through each row
of the inverted right-angled triangle. It starts with the first row and goes up to the specified number
of rows (`n`).
2. **First Inner Loop (`for (int j = i; j <= n; j++)`):** This loop is nested inside the outer loop and is
responsible for printing spaces before the asterisks. The number of spaces to be printed before the
asterisks is determined by the value of the outer loop variable `i`.

3. **Second Inner Loop (`for (int j = 1; j <= i; j++)`):** This loop is also nested inside the outer loop
and is responsible for printing the asterisks in each row. The number of asterisks to be printed in a
row is determined by the value of the outer loop variable `i`.

4. **Printing Spaces and Asterisks (`System.out.print(" ");` and `System.out.print("*");`):** Inside the
loops, it prints spaces before printing the asterisks to create the pattern.

5. **Move to the Next Line (`System.out.println(" ");`):** After printing spaces and asterisks in a row,
it moves to the next line to start a new row in the inverted right-angled triangle.

For `n = 5`, the output would be:

```

**

***

****

*****

```

This program generates a pattern where the number of asterisks decreases from top to bottom,
creating an inverted right-angled triangle.

Q4 The provided Java program prints a pattern of asterisks in the form of a right-angled triangle, but
this time it's oriented in a different way compared to the previous examples. Let's break down the
logic and algorithm:

```java

public class q4pattern {


public static void main(String[] args) {

int n = 5;

// Outer loop for the number of rows

for (int i = 1; i <= n; i++) {

// Inner loop to print spaces before asterisks

for (int j = 1; j <= i; j++) {

System.out.print(" ");

// Inner loop for printing asterisks in each row

for (int j = i; j <= n; j++) {

System.out.print("*");

// Move to the next line after printing spaces and asterisks in a row

System.out.println(" ");

```

Explanation:

1. **Outer Loop (`for (int i = 1; i <= n; i++)`):** This loop is responsible for iterating through each row
of the right-angled triangle. It starts with the first row and goes up to the specified number of rows
(`n`).

2. **First Inner Loop (`for (int j = 1; j <= i; j++)`):** This loop is nested inside the outer loop and is
responsible for printing spaces before the asterisks. The number of spaces to be printed before the
asterisks is determined by the value of the outer loop variable `i`.

3. **Second Inner Loop (`for (int j = i; j <= n; j++)`):** This loop is also nested inside the outer loop
and is responsible for printing the asterisks in each row. The number of asterisks to be printed in a
row is determined by subtracting the value of the outer loop variable `i` from `n`.
4. **Printing Spaces and Asterisks (`System.out.print(" ");` and `System.out.print("*");`):** Inside the
loops, it prints spaces before printing the asterisks to create the pattern.

5. **Move to the Next Line (`System.out.println(" ");`):** After printing spaces and asterisks in a row,
it moves to the next line to start a new row in the right-angled triangle.

For `n = 5`, the output would be:

```

*****

****

***

**

```

This program generates a pattern where the number of asterisks decreases from left to right,
creating a right-angled triangle.

q.5The provided Java program prints a pattern of asterisks in the form of an inverted right-angled
triangle with each row having double the number of asterisks. Let's break down the logic and
algorithm:

```java

public class q5pattern {

public static void main(String[] args) {

int n = 7;

// Outer loop for the number of rows

for (int i = 1; i <= n; i++) {

// Inner loop to print spaces before the first set of asterisks

for (int j = i; j <= n; j++) {


System.out.print(" ");

// Inner loop for printing the first set of asterisks in each row

for (int j = 1; j <= i; j++) {

System.out.print("*");

// Inner loop for printing the second set of asterisks in each row

for (int j = 1; j <= i; j++) {

System.out.print("*");

// Move to the next line after printing spaces and asterisks in a row

System.out.println();

```

Explanation:

1. **Outer Loop (`for (int i = 1; i <= n; i++)`):** This loop is responsible for iterating through each row
of the inverted right-angled triangle. It starts with the first row and goes up to the specified number
of rows (`n`).

2. **First Inner Loop (`for (int j = i; j <= n; j++)`):** This loop is nested inside the outer loop and is
responsible for printing spaces before the first set of asterisks. The number of spaces to be printed
before the asterisks is determined by the value of the outer loop variable `i`.

3. **Second Inner Loop (`for (int j = 1; j <= i; j++)`):** This loop is also nested inside the outer loop
and is responsible for printing the first set of asterisks in each row. The number of asterisks to be
printed in a row is determined by the value of the outer loop variable `i`.

4. **Third Inner Loop (`for (int j = 1; j <= i; j++)`):** This loop is also nested inside the outer loop and
is responsible for printing the second set of asterisks in each row. The number of asterisks to be
printed in a row is again determined by the value of the outer loop variable `i`.
5. **Move to the Next Line (`System.out.println();`):** After printing spaces and the two sets of
asterisks in a row, it moves to the next line to start a new row in the inverted right-angled triangle.

For `n = 7`, the output would be:

```

**

****

******

********

**********

************

**************

****************

```

This program generates a pattern where the number of asterisks increases from left to right, creating
an inverted right-angled triangle with each row having double the number of asterisks.

You might also like