JAVA Pyramids
In the last few tutorials we have discussed some basic concepts of programming and in next few posts we will be using them to make some simple programs.
Pyramids are very common examples that I think every programmer had made at the time of start learning programming. Pyramids are very useful in teaching the use of loops. There are numerous types of pyramids example. Lets start with an basic example.
Creating the Pyramid Program:- We will make an simple program that will take an integer input from user and print the asterisks pyramid with that much number of rows.
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
System.out.println("Enter the length of pyramid");
Scanner scan = new Scanner(System.in);
int rows = scan.nextInt();
System.out.println("-------Output------");
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
System.out.println("-------------------");
}
} |
In this program, first we take an integer input form user using
Scanner class and than nested for loop is declared (i.e. a loop inside another loop). In this the outer for loop will run the same number of times as enter by user(from 1 to the enter number) and after every iteration it will print a new row and increment the loop control variable by 1. Inside the outer loop a new inner for loop is declared which will print the same number of asterisk as the current iteration value of outer loop control variable.
Output of Program:-
Reverse Pyramid: The next program we will be making is to print a reverse pyramid. The reverse pyramid will look like:-
The code for printing a reverse pyramid is not very different from the previous one. Infact we can actually do it just by changing one line. Take a look at the code below.
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
System.out.println("Enter the length of pyramid");
Scanner scan = new Scanner(System.in);
int rows = scan.nextInt();
System.out.println("-------Output------");
for (int i = rows; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
System.out.println("-------------------");
}
} |
If you look at the code above, we have just made a slight change in the declaration of outer loop. This it the outer loop will start from the entered number and end at 1. After every iteration we decrease the value by one.
Output of Program:-
Some more examples of different Pyramids:-
If you look at this example, it can look a bit confusing, but it is not as much difficult as you might think. Lets take a look below.
----*
---**
--***
-****
***** |
Now its easy to understand the logic for this program. For this program we have to need to an extra for loop in our previous code of pyramid. Here is the code :
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
System.out.println("Enter the length of pyramid");
Scanner scan = new Scanner(System.in);
int rows = scan.nextInt();
System.out.println("-------Output------");
for (int i = 1; i <= rows; i++) {
for (int j = rows-1; j >= i; j--) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print("*");
}
System.out.println();
}
System.out.println("-------------------");
}
} |
Output of Program:-