February 26
February 26
Java NetBeans
XII PPLG
Authored by: Ja’far Ghifari A
1
Looping Left Up
import java.util.Scanner;
int rows = 5;
int i = 1;
while (j <= i) {
System.out.print("* ");
j++;
}
System.out.println();
i++;
}
}
}
run:
*
**
***
****
*****
BUILD SUCCESSFUL (total time: 0 seconds)
2
Looping Right Up
import java.util.Scanner;
public class loop3 {
public static void main(String[] args) {
int rows = 5;
int i = 1;
while (j > i) {
System.out.print(" ");
j--;
}
int k = 1;
while (k <= i) {
System.out.print("* ");
k++;
}
System.out.println(); i++;
}
}
}
run:
*
**
***
****
*****
BUILD SUCCESSFUL (total time: 0 seconds)
3
Looping Left Down
import java.util.Scanner;
int rows = 5;
int i = rows;
while (i >= 1) {
int j = 1;
while (j <= i) {
System.out.print("* ");
j++;
}
System.out.println();
i--;
}
}
}
run:
*****
****
***
**
*
BUILD SUCCESSFUL (total time: 0 seconds)
4
Looping Right Left
import java.util.Scanner;
int rows = 5;
int i = 1;
int j = 1;
while (j <= (rows - i + 1)) {
System.out.print("* ");
j++;
}
System.out.println();
i++;
}
}
}
run:
*****
****
***
**
*
BUILD SUCCESSFUL (total time: 0 seconds)
5
Looping Equilateral Triangels
import java.util.Scanner;
public class loop5 {
public static void main(String[] args) {
int rows = 5;
int i = 1;
int spaces = 1;
while (spaces <= rows - i) {
System.out.print(" ");
spaces++;
}
int j = 1;
while (j <= 2 * i - 1) {
System.out.print("* ");
j++;
}
System.out.println();
i++;
}
}
}
run:
*
***
*****
*******
*********
BUILD SUCCESSFUL (total time: 0 seconds)