Skip to content

Instantly share code, notes, and snippets.

@prameshbajra
Created June 7, 2018 13:20
Show Gist options
  • Save prameshbajra/5ebd081f0aa03f4f8bd3f2aec3ce1a52 to your computer and use it in GitHub Desktop.
Save prameshbajra/5ebd081f0aa03f4f8bd3f2aec3ce1a52 to your computer and use it in GitHub Desktop.
Diamond Pattern in java.
package com.demo.advjava;
public class StarsPattern {
public static void main(String[] args) {
printStars90(10);
printStarsRev(10);
// printStarsRev(10);
printStars(10);
}
static void printStars(int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j < i)
System.out.print("*");
else
System.out.print(" ");
}
for (int j = n - 1; j >= 0; j--) {
if (i >= j)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void printStars90(int n) {
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < n; j++) {
if (i <= j)
System.out.print("*");
else
System.out.print(" ");
}
for (int j = n - 1; j >= 0; j--) {
if (i >= j) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
System.out.println();
}
}
static void printStarsRev(int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j < i)
System.out.print(" ");
else
System.out.print("*");
}
for (int j = n - 1; j >= 0; j--) {
if (j > i)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment