Computer Science, asked by MrBrainlyBrilliant, 4 months ago

Write a program in Java to print the following pattern :-

*. *. * * 1
*. * * 1 2
* * 1 2 3
* 1 2 3 4
1 2 3 4 5

Note :- The symbol (*) is just for counting number of spaces. It is not to be printed. ​

Answers

Answered by anindyaadhikari13
9

Answer:

Here comes the Java c∅de for the question.

1. Using 3 loops.

public class Pattern {

public static void main(String[] args) {

int i,j;

for(i=1;i<=5;i++) {

for(j=1;j<=5-i;j++)

System.out.print(" ");

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

System.out.print(j+" ");

System.out.println();

}

}

}

Explanation:

  • Outer loop tells the number of rows for the pattern. As there are 5 rows, loop iterates in the range 1 to 5.

  • You can see that there are 2(5 - i) spaces in each row where i is the row number. For example, in the first row, i = 1, number of spaces = 2(5 - 1) = 8. But, here the loop iterates 4 times and after each iteration, two spaces are displayed, so total spaces = 8 (adjusted)

  • Next comes the numbers. There are 'i' numbers in each row where 'i' is the row number. For example, in the first row, there is only 1 number (1). In the next row, two numbers, (1 to 2) and so on. So, the third loop iterates i times > j = 1 to i > print(j). After displaying the numbers, a space is added.

  • After displaying the column, a new line is added by using println() statement.

2. Using 2 loops.

public class Pattern {

public static void main(String[] args) {

int i,j,a;

for(i=1;i<=5;i++) {

a=1;

for(j=1;j<=5;j++) {

if(j<=5-i)

System.out.print(" ");

else

System.out.print(a++ +" ");

}

System.out.println();

}

}

}

Explanation:

  • The above program is a modified version of the previous one. I have added a condition which determines the number of spaces to be added. Then, it prints the number.

  • As said earlier, there are 2(5 - i) spaces in each row, so, I have added the condition that - if(j<=5-i) is true or not. In that case, it print two spaces.

  • If the condition becomes false, the numbers are displayed.

  • I have initialised a = 1 in the beginning of the outer loop so that it gets updated and the numbers from 1 to 'i' are only displayed. After displaying the value of a, it's value is incremented.

  • A new line is displayed at the end of each row.

See the attachment for output.

Attachments:
Answered by atrs7391
3

/*

Project Type: Brainly Answer

Date Created: 17-02-2021

Date Edited Last Time: ---NIL---

Question Link: https://brainly.in/question/35384223

Question: Write a program in Java to print the following pattern :-

   1

  12

 123

1234

12345

Program Created By: atrs7391

Programming Language: Java

Language version (When program created or last edited): jdk-15.0.2

*/

package Brainly_Answers.Program;

public class Pattern_2 {

   public static void main(String[] args) {

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

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

               System.out.print(" ");

           }

           //this loop was to print spaces

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

               System.out.print(n);

           }

           //this loop was to print the numbers

           System.out.println();

       }

   }

}

Similar questions