# Printing the pyramid pattern of stars in Java: Video Lecture 15

Printing the pyramid pattern of stars is a common practice in learning any programming language. In this post, we will learn how to print the pyramid pattern of stars in Java. The program output for a pyramid of ten lines of stars is provided below.

```
My Computer$ java Pyramid
Enter number of lines: 10
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
```

## Video lecture explaining how to print a pyramid of stars in Java

The following video provides a detailed talk on how to print a pyramid pattern of stars in Java.

## Coming up with the solution

In the video above I discussed how we might draw a Pyramid pattern of stars in Java. I used loops to display the pyramid pattern of stars. I analyzed the problem from a problem-solving viewpoint and then wrote the code.

Drawing the pyramid pattern is widely used in teaching programming courses because it requires some analysis of the problem before we directly jump into the code.

### Example

```
*
***
*****
*******
*********
```

Notice in the pyramid above that we have

1 star in the first line,

3 stars in the second line,

5 stars in the third line,

7 stars in the fourth line,

And finally, there are 9 stars in the fifth line.

The pattern is such that the number of stars increases in consecutive lines. Our target is to use a loop to go over each line and print the stars.

### Going over lines

In my code, I will use a variable named *i*, to go over every line of stars. That is, “*i*” will vary from 1 to 5 for the above example-pyramid contains five lines of stars.

### Finding the relationship between a line and how many stars to print

For such a pattern-printing problem, it becomes beneficial if we can come up with a relationship between a line number *i* and how many stars we should print on that line. Let us try to find a connection between the line number and how many stars to print in that line. Let us iterate over the number of stars in the lines again.

When we are in line 1, we print 1 star.

In line 2, we print 3 stars.

When we are in line 3, we print 5 stars.

So and so forth.

The question we are trying to frame here is, when we are in the i*th* row, how many stars will we print?

### The formula for the number of stars in a line

Notice that the number of stars to be printed = 2*the line number we are currently in minus 1.

That is, if we are in the i*th* line, the number of stars to be printed is 2*i-1.

Let us check, if the hypothesis is correct for our running example.

When we are in the first line, i=1. Therefore, the number of stars in the first line should be 2*i-1=2*1-1=1. Therefore, we print one star in the first line.

When we are in the second line, i=2. Therefore, the number of stars in the second line should be 2*i-1=2*2-1=3. Therefore, we print three stars in the second line.

When we are in the third line, i=3. Therefore, the number of stars in the third line should be 2*i-1=2*3-1=5. Hence, we print five stars.

If we keep doing these calculations, we will see that the formula for the number of stars in the i*th* line, *2i-1* is correct for any line.

That is great! We have a formula for the number of stars for the i*th* line.

### How about the leading spaces

We have left out an important item in our analysis so far. Notice that, printing a pyramid of stars is not just printing stars in a pattern. What do I mean? In each line, we have some spaces before we start to print the stars of that line. The gap becomes smaller and smaller in subsequent lines. In the last line, there is no gap between the border and the first star of that line. These gaps in these lines are actually space-characters. That means we print some spaces before printing the stars in each line.

Let us analyze how many spaces we need to print before printing the stars in each line. That is, using the line number *i*, we will try to come up with a formula for number spaces in a line.

### Finding the relationship between a line number and number of spaces to print

You can use a paper and draw grids around the stars like the following one. Given that spaces are invisible drawing the grids will help us count the number of spaces in each line.

Now, let us count the spaces in each line.

We have 4 spaces in line 1.

There are 3 spaces in line 2.

We have 2 spaces in line 3

We have 1 space in line 4.

Line 5 contains zero space.

Notice that the number of leading spaces in a line is the total number of lines minus *i*, where i is the line number.

For example, let us take line 1. Here i=1. The number of spaces is 5-1=4.

In line 2, i=2. The number of spaces in the second line is 5-2=3.

In line 3, i=3. The number of spaces in the third line is 5-3=2.

In line 4, i=4. The number of spaces in the fourth line is 5-4=1.

In line 5, i=5. The number of spaces in the fifth line is 5-5=0.

### The formula for the number of spaces

Notice that for the ith line, if we subtract *i* from the total number of lines, we will get the number of leading spaces. Therefore, we can say, the number of spaces in line i is equal to *n-i*, where n refers to the total number of lines.

### Summary of the analysis

Based on our analysis, we have come up with two things.

If we are printing n lines of stars, then

- The number of leading spaces in the i
*th*line is*n-i*. - The number of stars in the i
*th*line is*2*i-1*.

We use these two pieces of information in our code.

## Pseudocode

Let us write a pseudocode for the solution first. Pseudocode is an informal coding to help design the actual program.

I will ask the user about how many lines the pyramid should contain.

Let us say, the user enters the number of lines in a variable named *n*.

Then I will write a loop that will iterate *n* times. To iterate, I will use a variable *i*. Inside the loop, *i* contains in which line the program is. Therefore, based on our discussion earlier, we can print *n-i* spaces first, and then we will print *2*i-1* stars. I will make sure to write a new line after printing al the required stars in a line. Here is the pseudocode.

That’s it. We have the structure of the code. Let us go for coding.

## The solution to printing a pyramid pattern

The code below provides a solution to printing a pyramid pattern. The program asks how many lines the pyramid should contain. Then it draws a pyramid containing a total number of lines provided by the user.

```
import java.util.Scanner;
class Pyramid{
public static void main(String[] args){
int n, i;
Scanner scan = new Scanner(System.in);
System.out.print("Enter number of lines: ");
n=scan.nextInt();
for (i=1; i<=n; i=i+1){
int sp;
for (sp=1; sp<=n-i; sp=sp+1){
System.out.print(" ");
}
int st;
for (st=1; st<=2*i-1; st=st+1){
System.out.print("*");
}
System.out.println();
}
}
}
```

In the pyramid problem, we are given *n*, the number of lines as the input. We use a Scanner object to get the number from the user.

Then, we write a for-loop to vary the value of variable *i*, from 1 to *n*.

Now, inside the loop, we will have to print the leading spaces. We write an inner loop to print *(n-i)* spaces. The loop variable is `sp`

. `sp`

will vary from 1 to *(n-i)*. We will just print a space in each iteration of this inner loop.

After printing the spaces, we have to print the stars. The number of stars we have to print is *2*i-1*. We use another loop that iterates *2*i-1* times. The loop variable is `st`

. Inside this loop, we will print a star.

Finally, after printing the spaces and the stars, we just write `System.out.println()`

without anything inside the parentheses to print a new line. We are done with the code.

Save the file, compile it, and run it. Enjoy the program.

## More exercises

Now that we know how to print a pyramid pattern os stars, we can use the knowledge to print other patterns. Write two programs for the following two patterns (Pattern A and Pattern B). The one on the right is slightly harder than the one on the left. Write the code for the left one first and then go for the right one. Enjoy!

## Concluding remarks

The purpose of the post is to demonstrate not only to demonstrate how we can print the pyramid pattern but also to demonstrate how to analyze a problem before diving into coding. For most real-life problems, you will see that analysis of the problem and coming up with a sketch of code help write the code quickly. In the given problem, printing the pyramid pattern of stars, I could not write the code without really doing the analysis. I had to figure out how the pattern is changing from one line to another. We need analysis for that.

The summary is,

- Please analyze the problem you are trying to solve before starting to code.
- Come up with a sketch of the solution on paper first.
- Then go for coding.

Many times, you will see that coding is the easy part. The analysis is a critical part of programming.

Anyway, if you haven’t subscribed to our website Computing4All.com and to the YouTube channel where we host our video lectures, please do so to receive notifications on our new articles and videos. Please do not hesitate to contact us via the comments section below. See you soon in another post.

i extremly like to be a good programmer as how am find your help

Hi, We will try our best to keep uploading videos and articles to help everyone. We have recently written an article on How to become a good programmer. We hope that the resources will be helpful.

Hello sir, i try my best to do exercise but in pattern B i didn’t find the exact solution. Please can you help me? Thank you!!!!

Absolutely. Would you please copy and paste the code you wrote so that we can take a look? Thank you.

sir. did i download and install a java and compiler

Yes, you will need to download Java. You can download JDK from this link: https://www.oracle.com/technetwork/java/javase/downloads/index.html

Our first video in this lecture series describes how to compile and run a program: https://computing4all.com/java/how-to-write-your-first-program-using-java/

Thank you!