Monday, April 2, 2012

Loop and Star Printing Programs

Dear reader,
I am writing this blog on a basic Loop and Star(*) printing in Java. Now these loop questions are asked only
at freshers level but recently one interviewer has asked me to do that. He has asked me 4 patterns to print
including Mirroring an Array Matrix. However I saved myself by replying 3 out of 4. Writing here the complete
program for me/your reference, hope it will help.

Programs written here, output is given too for help:
    1) Print a simple Pyramid, One sided and then Reverse one sided. This was not asked to me.
    2) Print a complete Pyramid which looks real, space between STARs. This was asked.
    3) Show a Mirror image of an Array. Row data should get reversed with their mirror images. This was asked.
    4) Printing a Numeric Pyramid using numbers. Again not asked.

==============Print a simple Pyramid, One sided and then Reverse one sided=================
//StarPrinting.java

public class StarPrinting {
    public static void main(String[] args) {
        int n=5;
        //One sided pyramid
        System.out.println("One sided pyramid:");
        for(int i=0;i<=n;i++) {
            for(int j=0;j<=i;j++){
                System.out.print("*");
            }
            System.out.println();
        }
        
        //Reverse Pyramid
        System.out.println("\nReverse pyramid:");
        for(int i=n;i>=0;i--) {
            for(int j=0;j<=i;j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
//Output:::::::::::::::
One sided pyramid:
*
**
***
****
*****
******

Reverse pyramid:
******
*****
****
***
**
*
==============Print a simple Pyramid, One sided and then Reverse one sided=================


==============Print a complete Pyramid which looks real, space between STARs===============
//OriginalPyramid.java

public class OriginalPyramid {
    public static void main(String[] args) {        
        int n=5; // Assign "n" any number
        
        //Both sided pyramid
        System.out.println("Both sided pyramid:");
        for(int i=0;i<=n;i++) {
            for(int j=i+n; j< (2*n); j++){
                System.out.print(" ");   //Print left side spaces
            }
            for(int k=0; k<i; k++){
                System.out.print("* ");  //Print Star and then followed by a space 
            }
            System.out.println();
        }
    }
}
//Output:::::::::::::::
Both sided pyramid:
     
    * 
   * * 
  * * * 
 * * * * 
* * * * * 
==============Print a complete Pyramid which looks real, space between STARs=================



==============Mirror image of an Array Matrix========================
/* Write a program to print Mirror image of below left side Array:
1 2 3  Mirror  3 2 1
4 5 6          6 5 4
7 8 9          9 8 7
10 11 12      12 11 10 
*/

//MirrorArray.java
public class MirrorArray {
      public static void main(String[] args) {
            int arr1[][]={ {1,2,3},{4,5,6},{7,8,9},{10,11,12}};
            int row=arr1.length;
            int col=arr1[0].length;
            int arr2[][]=new int[row][col];  //Here I will store mirror array elements.
            
            //Output array should have same no of rows and columns as master array. 
            System.out.println("Original Array elements:");
            for(int i=0;i<row;i++) {
                  for(int j=0;j<col;j++){
                        System.out.print(arr1[i][j]+" ");
                  }
                  System.out.println();
            }            
            System.out.println("Mirror Array elements:");
            for(int i=0;i<row;i++) {
                  for(int j=0;j<col;j++){
                        arr2[i][j]=arr1[i][col-(j+1)];
                        System.out.print(arr2[i][j]+" ");
                  }
                  System.out.println();
            }
      }
}
//Output::::::::::::
Original Array elements:
1 2 3 
4 5 6 
7 8 9 
10 11 12 
Mirror Array elements:
3 2 1 
6 5 4 
9 8 7 
12 11 10 
==============Mirror image of an Array Matrix========================


==============Numeric Pyramid========================
//PowerFactor.java
public class PowerFactor {
    public static void main(String[] args) {
        int n=4;
        int row;
        System.out.println("Numeric Pyramid:");
        for (row = 0; row <= n; row++){   //Prints each row 
            for (int spaces = n; spaces >= row; spaces --){ //Prints out double spaces to left
                System.out.print("  ");
            }

            int power1 = 0; //Power that 2 is being raised to
            for (int i=0; i<row; i++) { //Prints left side of the pyramid 
                System.out.print(" " + (int)Math.pow(2, power1));
                power1++;
            }

            int power2=row - 1;
            for (int i=1; i<row; i++) { //Prints right side of the pyramid 
                power2--;
                System.out.print(" "+(int)Math.pow(2, power2));       
            }
            System.out.println();
        }
    }
}

//Output:::::::::
Numeric Pyramid:
    
    1
  1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
==============Numeric Pyramid========================

No comments:

Post a Comment