πŸ’‘ Manmohan Loves Patterns- II

import java.util.;
import java.lang.
;
import java.io.*;
public class Main {
public static void main(String args[]) {
Scanner scn=new Scanner(System.in);
int k=0;
int n=scn.nextInt();
System.out.println(1);
for(int i=2;i<=n;i++)
{
System.out.print(i-1);
for(int j=1;j<=k;j++)
{
System.out.print(0);
}
System.out.print(i-1);
System.out.print("\n");
k++;
}
}
}
on compiling it shows
Exception in thread β€œmain” java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Main.main(Main.java:8)
what is the wrong in the above code

You need to take into account variables separately one for NO. OF ROWS.

Instead of using β€˜k’ for both ROWS and as a variable, take 2 separate variables.

int nst = 1;
int row = 1;
.
.
.
.
.
.
for (int cst = 1; cst <= nst - 2; cst++)
.
.
.
.
nst++;
System.out.println();
row++;

int nst = 1;
int row = 1;
.
.
.
.
.
.
for (int cst = 1; cst <= nst - 2; cst++)
.
.
.
.
nst++;
System.out.println();
row++;