Write a program to print the following
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Answers
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 code 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();
}
}
}
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
See attachment for output.
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();
}
}
}