Computer Science, asked by pputtegowda402, 13 days ago

Write a program to print the following
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5 ​

Answers

Answered by anindyaadhikari13
2

\texttt{\textsf{\large{\underline{Solution}:}}}

The given problem is solved using language - Java.

Here, I have assumed that the number of rows is fixed.

public class Pattern{

   public static void main(String args[]){

       int n=5,i,j;

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

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

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

           System.out.println();

       }

   }

}

If the number of rows is not fixed, do write the co‎de given below:

import java.util.*;

public class Pattern{

   public static void main(String args[]){

       int n,i,j;

       Scanner sc=new Scanner(System.in);

       System.out.print("Enter n - ");

       n=sc.nextInt();

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

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

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

           System.out.println();

       }

   }

}

\texttt{\textsf{\large{\underline{The O{u}tput}:}}}

1  

1 2  

1 2 3  

1 2 3 4  

1 2 3 4 5  

See attachment for output.

Attachments:
Answered by kamalrajatjoshi94
0

Answer:

Program:-

import java.util.*;

public class Main

{

public static void main(String args[])

{

Scanner in=new Scanner(System.in);

int n;

System.out.println("Enter no. of rows:");

n=in.nextInt();

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

{

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

{

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

}

System.out.println();

}

}

}

Attachments:
Similar questions