Write a program to print the following formation depending upon size of the matrix. Spaces between numbers are also important. Use System.out.println or System.out.print for printing.
Input (Integer) |
Printed Output |
4 |
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 |
5 |
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 |
class PrintFormation
{
public static void main(String s[])
{
printFormation(5);
}
public static void printFormation(int size)
{
}
}
Topic:
Nested for Loop In Java
If you need explanation Read this topic
If you need Answer Take test on this topic
User comments below. All of them might not be correct.
Guys , From the Question we can Confer that there is need for two loops one for printing from 1 through "size" with "space" between Numbers and other to repeat the "1st loop" for "size " number of times and need to move to "next line" .. So, take an outer loop starting at "1" and ending at "size" and simlarly inner loop starting at 1 and ending at "size" .. inside inner loop write a print statement with "inner loop variable followed by space " and close it and now use "print line statement " after inner loop and close outer loop... there it gives the Output Pattren
Posted by ?????????? ????? 2015-01-09 06:08:19
for(int outer = 1; outer <= 4; outer++)
{
for(int inner = 1; inner <= 4; inner++)
{
System.out.print(inner+" ");
}
System.out.println();
} In This question we have to use 2 loops 1 for coloumn and 1 for row.outer
Posted by Bhagi Bhagyasri 2015-01-09 09:43:03
Great Job ?????????? ????? , your comments are useful. They are precise and clear.
Posted by Merit Campus 2015-01-09 11:23:29
Bhagi Bhagyasri, thanks for your praticipation, but please avoid giving the complete code. Instead provide the hints. :-)
Posted by Merit Campus 2015-01-09 11:24:13
TO PRINT HE OUTPUT PATTERN SHOWN IN QUESTION "2 NESTED FOR LOOPS" SHOULD BE USED.
1.First Outer Loop which represents the no of lines for output should run from 1 till the length i.e. value passed as value.
for(int i=1;i<=4;i++)
2.second inner Loop which represents the no of elements in a line in output should run from 1 till the length i.e. value passed as value.
--Inside this loop value of counter should be printed with space to display the output as shown.
for(int j=1;j<=4;j++)
{
System.outprint(j+" ");
}
3.After each iteration of outer loop cursor should move to next line.
So,one println() Statement should be written inside the outer loop.
Posted by Mânïshå Mùlchåndânï 2015-01-09 16:43:36
This dose is now closed and the winners are ?????????? ?????, Mânïshå Mùlchåndânï, for 'First Correct Comment'. The 'lucky liker' is Gaurav Bansal. 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 2015-01-10 03:00:57