Don’t worry, unlock all articles / blogs on PrepInsta by just simply logging in on our website
JAVA Program for Pyramid Star Pattern
February 21, 2024
Printing Pattern
In this program we’re going to code pyramid star pattern program .
The logic of this program is this take any number input from user and store it in variable n and then run the for loop start from 0 to i
start from j=n-i to j– and again take another for loop start from j=0 to j++ to print stars and after this take line changement statement after the end of main for loop
Algorithm:
Enter the number as input from the user and store it in any variable.(‘n‘ in this case).
Run a loop ‘n’ number of times to iterate through each of the rows. From i=0 to i<n. The loop should be structured as for( i=0 ; i
Run a nested loop inside the main loop to print the spaces before the pyramid. From j=n-i to j–. The loop should be structured as for (int j=n-i; j>1; j–)
Inside this nested loop print white space System.out.print(” “); .
Run another nested loop inside the main loop after the previous loop to print the stars in each column of a row. From j=0 to j<=i. The loop should be structured as for (int j=0; j<=i; j++ )
Inside this nested loop print star System.out.print(” *”); .
Move to the next line by printing a new line . System.out.println();
Code in Java:
import java.util.Scanner;
public class Pattern1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter No");
int n=sc.nextInt();
for (int i=0; i<n; i++)
{
for (int j=n-i; j>1; j--)
System.out.print(" ");
for (int j=0; j<=i; j++ )
System.out.print(" *");
System.out.println();
}
}
}
This code is contributed by Shubham Nigam (Prepinsta Placement Cell Student)
The provided code doesn’t match the pattern exactly. The pattern is displaying odd number of stars in each subsequent rows, but your code is taking the stars as the number of rows. The correct code for the program will be:
Program:
public class PyramidTriangle {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter the height of triangle: “);
int rows = sc.nextInt();
//loop for rows
for(int i = 0;i0;j–)
{
System.out.print(” “);
}
for(int j = 0;j<(2*i+1);j++)
{
System.out.print("*");
}
System.out.println();
}
import java.util.*;
public class pattern{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print(“enter number of rows=”);
int n=sc.nextInt();
for(int i=0;i<n;i++){
for(int j=1;j<n-i;j++){
System.out.print(" ");
}
for(int j=0;j<2*i+1;j++){
System.out.print("*");
}
public static void main(String[] args) {
System.out.println(“Pattern!”);
for(int i=1;i<5;i++) {
for(int j=1;j<8;j++) {
if((i==2 &&(j==3 || j==5))||
(i==3 &&(j==2 || j==3 || j==5 || j==6))||
(i==4 &&(j==1 || j==2 || j==3 || j==5 || j==6 || j==7))
) {
System.out.print("*");
}
else if(j==4) {
System.out.print("*");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}
public class Pyramid{
public static void main(String[]args){
int n =5;
for(int i=0;i<n;i++){
for(int j=0;j<n-i;i++){
System.out.print(" ");
}
for(int j=1;j<=2*i+1;j++){
System.out.print("* ");
}
System.out.println(" ");
}
}
}
Why to complex the code by using count variable ?
The above code is right for the pyramid shape given below:
*
**
***
****
The correct code for the pattern given in the question above will be:
public class Pyramid {
public static void main(String[] args) {
int n=4;
for(int i=1;i<=n;i++){
for(int j=1;j<=n-i;j++){
System.out.print(" ");
}
for(int j=1;j<=(2*i-1);j++){
System.out.print("*");
}
System.out.println();
}
}
}
The provided code doesn’t match the pattern exactly. The pattern is displaying odd number of stars in each subsequent rows, but your code is taking the stars as the number of rows. The correct code for the program will be:
Program:
public class PyramidTriangle {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter the height of triangle: “);
int rows = sc.nextInt();
//loop for rows
for(int i = 0;i0;j–)
{
System.out.print(” “);
}
for(int j = 0;j<(2*i+1);j++)
{
System.out.print("*");
}
System.out.println();
}
import java.util.*;
public class pattern{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print(“enter number of rows=”);
int n=sc.nextInt();
for(int i=0;i<n;i++){
for(int j=1;j<n-i;j++){
System.out.print(" ");
}
for(int j=0;j<2*i+1;j++){
System.out.print("*");
}
System.out.println();
}
}
}