Program Pola Bintang menggunakan Java

Segitiga siku-siku kanan
import java.util.Scanner;

public class BintangSikuKanan {

public static void main(String args[]) {
Scanner db=new Scanner(System.in);
System.out.println("Masukkan batas baris: ");
int n=db.nextInt();
/*int n=5;*/
for (int i=1;i<=n;i++) {
for(int j=1;j<=i;j++) {
System.out.print("*");
}
System.out.println();
}
}
}



Segitiga siku-siku kiri
import java.util.Scanner;

public class BintangSikuKiri {
public static void  main(String []args) {
Scanner db=new Scanner(System.in);
System.out.println("Masukkan batas baris: ");
int n=db.nextInt();
int i,j,k;
for( i=1;i<=n;i++) {
//spasi
for( j=(n-1);j>=i;j--) {
System.out.print(" ");
}
for( k=1;k<=i;k++) {
System.out.print("*");
}
System.out.println();

}
}
}

Segitiga sama sisi
import java.util.Scanner;

public class BintangSegitigaSamaSisi {
public static void  main(String []args) {
Scanner db=new Scanner(System.in);
System.out.println("Masukkan batas baris: ");
int n=db.nextInt();

for(int i=1;i<=n;i++) {
//spasi
for(int j=(n-1);j>=i;j--) {
System.out.print(" ");
}
//segitiga siku-siku kanan
for(int k=1;k<=i;k++) {
System.out.print("*");
}
//segitiga siku-siu kiri yang mulai pada baris kedua
for(int l=2;l<=i;l++) {
System.out.print("*");
}
System.out.println();

}
}
}



Belah Ketupat
import java.util.Scanner;

public class BelahKetupat {
public static void main(String args[]) {
int a,b,c,d,e,f,g,h,i,j,k,l,m;
Scanner db=new Scanner(System.in);
System.out.println("Masukkan batas baris: ");
int n=db.nextInt();
for( a=1;a<=n;a++) {
//spasi
for( b=(n-1);b>=a;b--) {
System.out.print("-");
}
//segitiga siku-siku atas
for( c=1;c<=a;c++) {
System.out.print("*");
}
//segitiga siku-siku kiri samping
for(g=2;g<=a;g++) {
System.out.print("*");
}
//------------------spasi
for( i=(n-1);i>=a;i--) {
System.out.print("-");
}
//tempat nambah pola

System.out.println();
//segitiga siku-siku dibawah
if(a==n) {
for( d=1;d<=(n-1);d++) {
for( e=1;e<=d;e++) {
System.out.print("-");
}
//------------------------
for( f=(n-1);f>=d;f--) {
System.out.print("*");
}
for( h=(n-1);h>=d+1;h--) {
System.out.print("*");
}
//spasi--------------------
for( e=1;e<=d;e++) {
System.out.print("-");
}

System.out.println();
}
}
}


}
}



DuaBuahSegitigaSamaSisi

public class DuaBuahBintangSegitigaSamaSisi {
public static void main(String args[]) {
int n=5;
for(int i=1;i<=n;i++) {
//spasi
for(int j=4;j>=i;j--) {
System.out.print(" ");
}
//segitiga siku-siku kanan
for(int k=1;k<=i;k++) {
System.out.print("*");
}
//segitiga siku-siu kiri yang mulai pada baris kedua
for(int l=2;l<=i;l++) {
System.out.print("*");
}
//spasi
for(int m=4;m>=i;m--) {
System.out.print(" ");
}
for(int p=4;p>=i;p--) {
System.out.print(" ");
}
//segitiga siku-siku kanan
for(int o=1;o<=i;o++) {
System.out.print("*");
}
//segitiga siku-siku kiri
for(int q=2;q<=i;q++) {
System.out.print("*");
}
System.out.println();
}
}
}

Komentar

Postingan populer dari blog ini

ALAMAT PERUSAHAAN ASING DI INDONESIA

preparing oil and gas company

Best JavaScript Debugging Tools