Challenging Numerical Triangles
by CodeChum Admin
You probably have encountered Triangle pattern problems before when you started learning programming. Let’s see how well you do with numbers this time. Try to look at the examples closely and think of an algorithm that replicates the outputs. Go for it!
Input
A single line containing an integer n.
9
Output
The output generated based on the inputted integer n.
1
23
456
789
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int line = 1;
int counter = line;
for (int i = 1; i <= n; i++) {
System.out.print(i);
if (--counter == 0) {
counter = ++line;
System.out.println();
}
}
}
}
Comments
Leave a comment