Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i make it so that spaces generate so the outcome is different

I would like the output to have 9 spaces then a star, 8 spaces then two stars, 7 spaces and 3 stars and so on until 10 stars at the bottom. How can I generate the spaces?

import java.util.Scanner;
public class ThreeDotTwelvea
{
    public static void main(String args[])
    {
        final int max_rows = 1;
        for (int row = 10; row  >= max_rows; row--)
        {
            for (int star = 1; star <= row; star++)
                System.out.print ("*");
            System.out.println();
        }
    }
}

1 Answers

try this

public class ThreeDotTwelvea {

    final static int NO_OF_ROWS = 10;

    public static void main(String[] args) {

        for (int i = 0; i < NO_OF_ROWS; i++) {
            for (int j = NO_OF_ROWS - i - 1; j > 0; j--) {
                System.out.print(" ");
            }
            for (int j = 0; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

OUTPUT

Output

like image 138
Johny Avatar answered Dec 11 '25 17:12

Johny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!