Menu
Topics Index
...
`

Print Symbol Formation


If you need explanation Read this topic

If you need Answer Take test on this topic

Write a program to print the following formation depending upon size of the matrix. Use System.out.println or System.out.print for printing.

Input (Integer) Printed Output
4
- - 
* * * 
- - - -
5
- - 
* * * 
- - - - 
* * * * *
6
- - 
* * * 
- - - - 
* * * * *
- - - - - -



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.

start   forloop(1) from 1 to size             condition:for(i=1;i<=size;i++)

every iteration  in for loop check a condition if i value is odd
print '*'character (i times using inner forloop );
if 'i' is even then print '-'character for(i times using inner for loop)
and every iteration goto next line
innerforloop condition is(for(j=1;j<=i;j++){print(character);}

logic:
int i,j;
char c;
for(i=1;i<=size;i++)
{
  if(i%2==1)
    c='*';
  else
   c='-';
  for(j=1;j<=i;j++)
   {System.out.print(c+"  ");}

  System.out.println();//goto next line
}

Posted by Goutham Singarapu    2015-02-04 06:15:19


to print the pattern we have to use nested for loops... the outer for loop should be iterated for the number of rows(i.e., rows = input) and the inner for loop will print either * or - based on the row number(if row is odd print '*' else print '-').. rows should count from 1 to input... and after executing inner for loop print a new line to get the pattern..

Posted by Gopinath Manchikanti    2015-02-04 06:27:51


we need nested for loop to printing this pattern,outer for loop iterates upto given size.each time inner for loop executes depends on outer loop variable. example outer variable i =1,i<=3, i ++ ,thn inner j=1 , j<= i,j++,to printing required pattern check outer variable i value even or odd,if it is odd use * ,otherwise use "-"

Posted by Maheshwari Natarajan    2015-02-05 02:08:57


This dose is now closed and the winners are Gopinath Manchikanti,  for 'First Correct Comment', Maheshwari Natarajan,  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    2015-02-05 07:37:37


© meritcampus 2019

All Rights Reserved.

Open In App