Pyramid pattern program in Java
xxxxxxxxxx
public 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();
}
}
}
Explore the Source of this Code snippet to learn a lot more about such pattern programs.
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
// pyramid pattern - TutorialsTonight
public 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();
}
}
}
/* Output:
*
***
*****
*******
*********
*/
xxxxxxxxxx
public class reversePyramid {
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 < i; j++) {
System.out.print(" ");
}
// print stars
for (int k = 0; k < 2 * (size - i) - 1; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
Explore the source of this Code snippet to learn a lot more pattern programs in Java
xxxxxxxxxx
import java.io.*;
// Java code to demonstrate star pattern
public class GeeksForGeeks
{
// Function to demonstrate printing pattern
public static void printNums(int n)
{
// initialising starting number
int i, j, num=1;
// outer loop to handle number of rows
// n in this case
for(i=0; i<n; i++)
{
// without re assigning num
// num = 1;
for(j=0; j<=i; j++)
{
// printing num with a space
System.out.print(num+ " ");
// incrementing num at each column
num = num + 1;
}
// ending line after each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 5;
printNums(n);
}
}
xxxxxxxxxx
public 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();
}
}
}
Visit the source of this code snippet to learn more pattern program in java
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
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *