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
Or
Input
4
Output
1
23
4
Or input
12
Output
1
23
456
78910
1112
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
int x = 1, n = 1;
while (x <= number) {
for (int i = 0; i < n; i = i + 1)
if (x <= number)
System.out.print(x++);
System.out.println();
n = n + 1;
}
}
}
Comments
Leave a comment