Skip to content

Instantly share code, notes, and snippets.

@julianjupiter
Last active October 16, 2017 16:36
Show Gist options
  • Save julianjupiter/0436dbb66a9727c5a8b83e425bd2ad7f to your computer and use it in GitHub Desktop.
Save julianjupiter/0436dbb66a9727c5a8b83e425bd2ad7f to your computer and use it in GitHub Desktop.
Triangle in Java While
public class While {
public static void whileLoop1() {
int columnCounter = 5;
while (columnCounter >= 1) {
int rowCounter = 1;
int space = 5 - columnCounter;
while (space > 0) {
System.out.print(" ");
space--;
}
while (rowCounter <= columnCounter) {
System.out.print("*");
rowCounter++;
}
System.out.println();
columnCounter--;
}
}
public static void whileLoop2() {
int columnCounter = 1;
while (columnCounter <= 5) {
int rowCounter = 1;
while (rowCounter <= columnCounter) {
System.out.print("*");
rowCounter++;
}
System.out.println();
columnCounter++;
}
}
public static void whileLoop3() {
int columnCounter = 1;
while (columnCounter <= 5) {
int rowCounter = 5;
while (rowCounter >= columnCounter) {
System.out.print("*");
rowCounter--;
}
System.out.println();
columnCounter++;
}
}     
public static void whileLoop4() {
int columnCounter = 1;
while (columnCounter <= 5) {
int rowCounter = 1;
int space = 5 - columnCounter;
while (space > 0) {
System.out.print(" ");
space--;
}
while (rowCounter <= columnCounter) {
System.out.print("*");
rowCounter++;
}
System.out.println();
columnCounter++;
}
}
public static void main(String[] args) {
whileLoop1();
System.out.println();
whileLoop2();
System.out.println();
whileLoop3();
System.out.println();
whileLoop4();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment