info@meritcampus.com    +91-85006-22255
...
`

Write a program to print the following formation depending upon size of the matrix. Spaces at the beginning of each line are important. Use System.out.println or System.out.print for printing.

Input (Integer) Printed Output
4 `      *     # #   * * * # # # #`
7 `            *           # #         * * *       # # # #     * * * * *  # # # # # # * * * * * * *`

```      class PrintFormation {     public static void main(String s[])    {        printFormation(5);    }     public static void printFormation(int size)     { ```
```     } } ```
Topic:

### User comments below.All of them might not be correct.

here we have the pattern in the form of rows and columns...and here we are not filling the rows  full...

here we need to use the nested loops Bcz we have pattern in the form of rows and cols...

here take 3 loop controls i,j,k..we need to use 2 loops inside the outer loop...
initialize the i with 1 and put condition until it reaches the size...coming to inner for loop here we need to print space in every row so use for loop to print the spaces in rose..spaces should look like this in 1st row--we have 4(size-row num)
2nd row..3(size-row num)..so here we can put condition that j<=size-i..here i will be row number j must start from 1

now we here we have combination of bothe * and # if you look at the pattern * are placed in the 1,3,5,7 where as # are in 2,4,6 etc..so we can say the if the row is even than we should print # else *
put k=1 and repeat until it reaches row num  means k<=i..if i is even(i%2==0) than print # else *

Posted by Uday Kumar    2014-12-01 06:07:11

here we have to print th pattrn in rows and column..... th cndtn is that pattrn shud b in right triangular form... her toprnt this pattrn.. frstly we will tk an outer loop whose valu strts frm 1 to size of pattrn i.e i=1 to size..... dis loop is for rows den we will take anothr inner loop to prnt column values... here we can see that in frst row thr r 3 spaces and den * is prntd.. in scnd row 2 spaces n den 2#r prntd n so on... so frstly we hv to tk a loop to prnt spaces. here inside i's loop we r tkng a j loop whose valuewill b frm 1 to size n den inside dis loop we will chk if j<size-i den prnt spaces else chk if i%2=0 then prnt # othrwise prnt *...since we can see tht in odd rows *is prntd n in even rows # is prntd hence we r chkng th value of I whthr even or odd to prnt #n*.

Posted by Asma Mujtaba Khan    2014-12-01 06:36:32

This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Asma Mujtaba Khan,  for 'Second Correct Comment'. The 'lucky liker' is Sai Ram. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-12-02 04:46:28