how do i write a java code preferably using the 'while-do', 'if-else', 'for' statements to get these outcomes?

n is the input and the system prints triangles of height n?


n=2

. *
***

n=5

........*
......***
....*****
..*******
*********

ignore the dots, they are to align them properly

thanks in advance

Dani AI

Generated

A short, practical summary and a compact while-loop example.

The task is to print a centered pyramid of height n (base width = 2*n - 1). asked for use of while / if-else / for. already showed a couple of for-loop approaches (the second produces the centered pyramid); 's note about assignment etiquette is worth keeping in mind if this is course work.

Key idea: each row reduces left padding by one and increases the visible stars by two (start with 1 star on the top). Validate the input (existence and positivity) to avoid exceptions, and avoid trailing spaces if exact alignment is important.

public class Pyramid {
    public static void main(String[] args) {
        if (args.length == 0) {
            System.err.println("Usage: java Pyramid <positive integer>");
            return;
        }
        int n;
        try {
            n = Integer.parseInt(args[0]);
        } catch (NumberFormatException e) {
            System.err.println("Argument must be an integer");
            return;
        }
        if (n <= 0) {
            System.err.println("n must be > 0");
            return;
        }

        int row = 0;
        while (row < n) {
            int pad = n - row - 1;        // left spaces
            int stars = 2 * row + 1;     // odd number of stars

            int i = 0;
            while (i < pad) { System.out.print(" "); i++; }
            i = 0;
            while (i < stars) { System.out.print("*"); i++; }
            System.out.println();
            row++;
        }
    }
}

Notes: for large n build each line with StringBuilder (or String.repeat on Java 11+) to reduce many small prints; replace the pad character with '.' when debugging alignment. If this is an assignment, prefer posting an attempt and asking for hints rather than a full solution—'s point.

Recommended Answers

All 2 Replies

public class Triangle {

    public static void main(String[] args) { 
        int N = Integer.parseInt(args[0]);

        // Loop N times, one for each row
        for (int i = 0; i < N; i++) {

            // Print j periods
            for (int j = 0; j < i; j++)
                System.out.print(". ");

            // Print N-i asterisks
            for (int j = 0; j < N - i; j++)
                System.out.print("* ");

            // print a new line
            System.out.println();
        }
    }
}
public class Triangle {

    public static void main(String[] args) { 
        int N = Integer.parseInt(args[0]);

for (int i = 0; i < N; i++)
        {
            int stars = 1 + 2 * (i);
            int space = N - i;
            for (int j = 0; j < space; j++)
            {
                System.out.print("  ");
            }
            
            for (int k = 0; k < stars; k++)
            {
                System.out.print("* ");
            }
            System.out.println();
        }
commented: This forum is not for you to do others people homework -1

Hi ordi. You are new here so you must learn that you shouldn't give the code for others people assignments. This is not that kind of forum.

When someone's post their assignment and nothing more don't give the complete solution. It violates the rules.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.