xxxxxxxxxx
public class Patterns {
public static void main(String[] args) { starPyramid(5);}
static void starPyramid(int n) {
int cols = 2*n-1; // if n = 5 => ----*---- and n is number of rows
int midIndex = cols/2; // or n-1 => mid will be always the same, only the pyramid width increases in each row by 1 on each side
for(int i=0; i<n; i++){
int fillStart = midIndex - i;
int fillEnd = midIndex + i;
//iterating through all the columns
for(int j=0; j<cols; j++){
// filling the stars in current row pyramid width
if(j>=fillStart && j<=fillEnd)
System.out.print("*");
else System.out.print("-");
}
System.out.println();
}
}
// for n = 5
----*----
---***---
--*****--
-*******-
*********
}
xxxxxxxxxx
class pyramid {
// pyramid star pattern
public static void main(String[] args) {
int size = 5;
for (int i = 0; i < size; i++) {
// print spaces
for (int j = 0; j < size - i - 1; j++) {
System.out.print(" ");
}
// print stars
for (int k = 0; k < 2 * i + 1; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
xxxxxxxxxx
class HelloWorld {
public static void main(String[] args){
//Scanner sc = new Scanner(System.in);
int n = 4;//sc.nextInt();
int maxCol = 2*n+1;
int mid = maxCol/2;
for(int i=0; i<n; i++){
int fillStart = mid - i;
int fillEnd = mid + i;
//iterating through columns: max columns = 2*n+1
for(int j=0; j<maxCol; j++){
if(j >= fillStart && j<= fillEnd){
System.out.print("*");
}
else{
System.out.print("-");
}
}
System.out.println();
}
}
}
xxxxxxxxxx
class hollowPyramid {
public static void main(String[] args) {
// size of the pyramid
int size = 5;
for (int i = 0; i < size; i++) {
// print spaces
for (int j = 0; j < size - i - 1; j++) {
System.out.print(" ");
}
// print stars
for (int k = 0; k < 2 * i + 1; k++) {
if (i == 0 || i == size - 1) {
System.out.print("*");
}
else {
if (k == 0 || k == 2 * i) {
System.out.print("*");
}
else {
System.out.print(" ");
}
}
}
System.out.println();
}
}
}
xxxxxxxxxx
public class Patterns {
public static void main(String[] args) { starPyramid(5);}
static void starPyramid(int n) {
int cols= 2*n-1; // if n = 5 => ----*---- and n is number of rows
System.out.println("Star Pyramid");
for (int i = 1; i <= n; i++) {
for (int j = 1, noOfStars = 0; j <= cols; j++) {
// pyramid starting column in this row
int startCol = n-i+1; // or (int)Math.ceil(cols*1.0/2) - i+1;
int noOfStarsInThisRow = 2*i-1;
if(startCol== j-noOfStars && noOfStars < noOfStarsInThisRow){
noOfStars++;
System.out.print("*");
} else System.out.print(".");
}
System.out.println(); // new line
}
}
//for n=5
* . .
***
..*****..
.*******.
*********
}
xxxxxxxxxx
public static void StarPyramid(int n)
{
for (int i = 0; i < n; i++)
{
var bol = true;
for (int j = 0; j < 2*n; j++)
{
if(j+i>=n && j<=n+i)
{
if(bol)System.out.print("*");
else System.out.print(" ");
bol=!bol;
}
else System.out.print(" ");
}
System.out.println();
}
}
//for n =7
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *