Skip to content

Instantly share code, notes, and snippets.

@prashanth-g
Created December 26, 2016 07:58
Show Gist options
  • Select an option

  • Save prashanth-g/8caaa5bdabed7b5d882335d5f76b3477 to your computer and use it in GitHub Desktop.

Select an option

Save prashanth-g/8caaa5bdabed7b5d882335d5f76b3477 to your computer and use it in GitHub Desktop.
package javapractices;
public class Stars {
public static void main(String[] args) {
int numberOfRows = 5;
for (int numberOfTimes = 1; numberOfTimes <= numberOfRows ; numberOfTimes++) {
for (int numberOfSpaces = 0; numberOfSpaces < numberOfRows - numberOfTimes; numberOfSpaces++) {
System.out.print(" ");
}
for (int numberOfStars = 0; numberOfStars < 2 * numberOfTimes - 1; numberOfStars++) {
System.out.print("*");
}
System.out.print("\n");
}
}
}
@prashanth-g
Copy link
Author

// this can work

package javapractices;
public class Stars {
    public static void main(String[] args) {
        int max = 5; // rows
        int temp = max;
        // num rows
        for (int i = 0 ; i < max ; i++ ) {
            // num of spaces
            for (int j = 0; j < temp - 1; j ++) {
                System.out.print(" ");
            }
            // num of stars
            for (int k = 0; k < (2*i + 1); k++) {
                System.out.print("*");
            }
            System.out.print("\n");
            temp--;
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment