Nested for loop to print the pyramids in java

Posted By: Java Examples - 10:13 PM

Share

& Comment

This program to print each of the the following patterns use nested for loop 

  • Pyramid 1 :
  • /**
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package loop;
    
    /**
     *
     * @author ACHCHUTHAN
     */
    public class Pyramids {
    
        public static void main(String[] args) {
            for (int i = 1; i <= 5; i++) {
                for (int j = 1; j <= i; j++) {
                    System.out.print("#");
                }
                System.out.println("");
            }
    
    }
    
    
    Output of this program :
      run:
      #
      ##
      ###
      ####
      #####
      BUILD SUCCESSFUL (total time: 1 second)
  • Pyramid 2 :
  • /**
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package loop;
    
    /**
     *
     * @author ACHCHUTHAN
     */
    public class Pyramids {
    
        public static void main(String[] args) {
            
            for (int i = 5; i >= 0; i--) {
                for (int s = 1; s < i; s++) {
                    System.out.print(" ");
                }
                for (int j = 5; j >= i; j--) {
                    System.out.print("#");
                }
                System.out.println("");
            }
        }
    }
    
    
    
    Output of this program :
      run:
          #
         ##
        ###
       ####
      #####
      ######
      BUILD SUCCESSFUL (total time: 1 second)
  • Pyramid 3 :
  • /**
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package loop;
    
    /**
     *
     * @author ACHCHUTHAN
     */
    public class Pyramids {
    
        public static void main(String[] args) {
    
            for (int i = 1; i <= 5; i++) {
                for (int s = 1; s < i; s++) {
                    System.out.print(" ");
                }
                for (int j = 5; j >= i; j--) {
                    System.out.print("#");
                }
                System.out.println("");
            }
        }
    }
    
    
    Output of this program :
      run:
      #####
       ####
        ###
         ##
          #
      BUILD SUCCESSFUL (total time: 1 second)

  • Pyramid 4 :
  • /**
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package loop;
    
    /**
     *
     * @author ACHCHUTHAN
     */
    public class Pyramids {
    for (int i = 1; i <= 5; i++) {
                for (int s = 5; s > i; s--) {
                    System.out.print(" ");
                }
                for (int j = 1; j < i; j++) {
                    System.out.print("#");
                }
                for (int j = 1; j < i; j++) {
                    System.out.print("#");
                }
                System.out.println("");
            }
            for (int i = 1; i <= 5; i++) {
                for (int s = 1; s < i; s++) {
                    System.out.print(" ");
                }
                for (int j = 5; j > i; j--) {
                    System.out.print("#");
                }
                for (int j = 5; j > i; j--) {
                    System.out.print("#");
                }
                System.out.println("");
            }
        }
    }
    
    
    Output of this program :
        run:
        
           ##
          ####
         ######
        ########
        ########
         ######
          ####
           ##
        
        BUILD SUCCESSFUL (total time: 1 second)

About Java Examples

I’m passionate about Web Development and Programming and I go to extreme efforts to meet my passion. I’m a believer of learning the fundamentals first. I try to understand everything little bit more than the average.

Copyright © 2016 Java Examples ACHCHUTHAN.ORG. Designed by Templateism .