Computer Science, asked by sandrasony17, 1 year ago

Write a java program to get the following output
1
22
333
4444
55555

Answers

Answered by raviravi95
8



ELECTROFRIENDS.COMSOURCE CODESSOFTWARE PROGRAMSJAVABASIC PROGRAMSJAVA PROGRAM TO GENERATE A TRIANGLE 1 22 333 444

Java program to generate a Triangle 1 22 333 444

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 /*Write a program to generate a Triangle. eg: 1 2 2 3 3 3 4 4 4 4 and so on as per user given number */ class Triangle{ public static void main(String args[]){ int num = Integer.parseInt(args[0]); for(int i=1;i<=num;i++){ for(int j=1;j<=i;j++){ System.out.print(" "+i+" "); } System.out.print("\n"); } }

Answered by tanupriyatommy
19

Answer:

public class Sample {

   public static void main(String[] args) {

       int n=5;

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

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

               System.out.print(i);

           }

           System.out.print("\n");

       }

   }

}

Explanation:

In these kind of examples, first we need to think of rows and colums.

In our example, we are displaying six rows and each row is displaying it’s row number in row number of times.

The inner for loop is used to print the complete row.

If i = 1, the inner for loop is printing 1, if i = 2, the inner for loop is printing 2 two times and so on…

Whenever the inner for loop completes, we are moving to next line.

Similar questions