0% found this document useful (0 votes)
18 views6 pages

February 26

The document discusses Java code examples for different types of looping patterns that output star shapes including: looping left up, looping right up, looping left down, looping right left, and looping equilateral triangles.

Uploaded by

akoiko02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views6 pages

February 26

The document discusses Java code examples for different types of looping patterns that output star shapes including: looping left up, looping right up, looping left down, looping right left, and looping equilateral triangles.

Uploaded by

akoiko02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Tugas Looping

Java NetBeans

DATE \@ "MMMM d" \* MERGEFORMAT


February 26

XII PPLG
Authored by: Ja’far Ghifari A

1
Looping Left Up

import java.util.Scanner;

public class loop {


public static void main(String[] args) {

int rows = 5;
int i = 1;

while (i <= rows) {


int j = 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 (i <= rows) {


int j = rows;

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;

public class loop4 {


public static void main(String[] args) {

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;

public class loop2 {


public static void main(String[] args) {

int rows = 5;
int i = 1;

while (i <= rows) {


int space = 1;
while (space < i) {
System.out.print(" ");
space++;
}

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;

while (i <= rows) {

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)

You might also like