Friday, October 26, 2012

Use "FOR" loop to create "PYRAMID" !!


This is the second "for" loop program to create design, in this post we are going to create the "Pyramid".

What do you think, Is it complicated or not? 

Of course not !!!!!!!

    
                   ^
                  ^ ^
                 ^ ^ ^
                ^ ^ ^ ^
               ^ ^ ^ ^ ^
              ^ ^ ^ ^ ^ ^
             ^ ^ ^ ^ ^ ^ ^
            ^ ^ ^ ^ ^ ^ ^ ^
           ^ ^ ^ ^ ^ ^ ^ ^ ^
          ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
         ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
        ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
       ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
      ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
     ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
    ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
   ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
  ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
 ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^


Here is the code........................................


public class Pyramid {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int height = 20; 

for(int x=1;x<=height;x++){
for(int y=1;y<=(height-x);y++){
System.out.print(" ");
}
for(int z=1;z<x;z++){
System.out.print("^ ");
}
System.out.print("^");
System.out.println();
}
}
}


You can replace "^" with any other symbol you like. And also try to do debug of this program to understand it step by step.

No comments:

Post a Comment