Thursday, January 26, 2012

JAVA Programming : Pyramids

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:-

For more pyramid examples go to : JAVA Pyramids 2 

11 comments:

  1. how to get code like this

    1
    1 2
    1 2 3
    1 2 3 4
    1 2 3 4 5

    ReplyDelete
  2. how about this code?

    12345
    -1234
    --123
    ---12
    ----1

    ReplyDelete
  3. Check this one to form different patterns of pyramid
    http://itschoolstudents.blogspot.com/

    ReplyDelete
  4. How about this figure?
    *
    ***
    *****
    *******

    ReplyDelete
  5. mayo clinic diet fatty liver disease mayo clinic
    diet fatty liver disease mayo clinic diet fatty liver disease

    Also visit my weblog - fatty liver cure and diet

    ReplyDelete
  6. how to write a simple code for this?

    *****
    ***
    *

    ReplyDelete
  7. http://javainterviewcracker.blogspot.in/

    ReplyDelete