Computer Science, asked by ziadalsaydia, 3 months ago

Q/write a java program togenerate the following output?
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

Answers

Answered by anindyaadhikari13
2

Correct Question:

Write Java program to generate the following output.

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

Solution:

The given códe is written in Java.

public class Main {

 public static void main(String[] args) {

   int i,j;

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

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

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

      System.out.println();

   }

 }

}

Explanation:

  • Initially, i = 1. So, inner loop iterate 1 time and displays 1 on the screen. Again, when i = 2, inner loop iterates 2 times and displays 1 2 on the screen.
  • This continues till the value of i is less than 7. When i becomes greater than 7, loop is terminated.
  • After displaying each row, a new line is inserted.

See the attachment for output.

•••♪

Attachments:
Answered by BrainlyProgrammer
1

Correct Pattern:-

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

Your Answer:-

public class Ptt{

public static void main(String[] args) {

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

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

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

System.out.println();

}

}

}

Variable Description:-

  • i :- loop variable
  • j:- loop variable
Similar questions