Answer to Question #289423 in Java | JSP | JSF for Kev

Question #289423

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

1
Expert's answer
2022-01-22T02:42:05-0500
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();
            }
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog