Friday, January 27, 2012

JAVA Programming : Pyramids 2

JAVA Pyramids 2


In the previous post we have discussed about different pyramids examples and we will continue to do some more examples. In this tutorial I will post the code of some more complex pyramid examples.

         *
        ***
       *****
      *******
     *********      

Code for this program:-

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("*");
      }
      for (int l = 2; l <= i; l++) {
        System.out.print("*");
      }
      System.out.println();
    }
    System.out.println("-------------------");
  }
}

Output of Program:-


      ************     
      *****  *****
      ****    ****
      ***      ***
      **        **
      *          *

Code for this Program:-

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; j >= i; j--) {
        System.out.print("*");
      }
      for (int k = 2; k <= i; k++) {
        System.out.print("  ");
      }
      for (int l = rows; l >= i; l--) {
        System.out.print("*");
      }
      System.out.println();
    }
    System.out.println("-------------------");
  }
}

Output of Program:-


     ************     
     *****  *****
     ****    ****
     ***      ***
     **        **
     *          *
     *          *
     **        **
     ***      ***
     ****    ****
     *****  *****
     ************

Code for this Program:-


2 comments:

  1. Thank for your help and make it many program

    ReplyDelete
  2. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.

    ReplyDelete