Skip to content

Instantly share code, notes, and snippets.

@julianjupiter
Last active October 17, 2017 02:53
Show Gist options
  • Save julianjupiter/a6bc957f6737a0f10488878832387bfe to your computer and use it in GitHub Desktop.
Save julianjupiter/a6bc957f6737a0f10488878832387bfe to your computer and use it in GitHub Desktop.
Triangle in Java Do While
public class DoWhile {
public static void doWhileLoop1() {
int columnCounter = 5;
do {
int rowCounter = 1;
int space = 5 - columnCounter;
do {
System.out.print(" ");
space--;
} while (space > 0);
do {
System.out.print("*");
rowCounter++;
} while (rowCounter <= columnCounter);
System.out.println();
columnCounter--;
} while (columnCounter >= 1);
}
public static void doWhileLoop2() {
int columnCounter = 1;
do {
int rowCounter = 1;
do {
System.out.print("*");
rowCounter++;
} while (rowCounter <= columnCounter);
System.out.println();
columnCounter++;
} while (columnCounter <= 5);
}
public static void doWhileLoop3() {
int columnCounter = 1;
do {
int rowCounter = 5;
do {
System.out.print("*");
rowCounter--;
} while (rowCounter >= columnCounter);
System.out.println();
columnCounter++;
} while (columnCounter <= 5);
}
public static void doWhileLoop4() {
int columnCounter = 1;
do {
int rowCounter = 1;
int space = 5 - columnCounter;
do {
System.out.print(" ");
space--;
} while (space > 0);
do {
System.out.print("*");
rowCounter++;
} while (rowCounter <= columnCounter);
System.out.println();
columnCounter++;
} while (columnCounter <= 5);
}
public static void main(String[] args) {
doWhileLoop1();
System.out.println();
doWhileLoop2();
System.out.println();
doWhileLoop3();
System.out.println();
doWhileLoop4();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment