0

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

Edited by shredded: n/a

3
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by javaAddict
0
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();
        }

Edited by -ordi-: n/a

Votes + Comments
This forum is not for you to do others people homework
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.