Menu
Topics Index
...
`

Print Symbol Number 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. Spaces at the starting of the line and spaces in between are important. Use System.out.println or System.out.print for printing.

Input (Integer) Printed Ouput
4       * 
    @ @ 
  3 3 3 
@ @ @ @
5         * 
      @ @ 
    * * * 
  4 4 4 4 
* * * * *
6           * 
        @ @ 
      * * * 
    @ @ @ @ 
  5 5 5 5 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.

To print the pattern as shown in Program output "Nested For" Loops will be used.

--1st outer loop will run from 1 to no passed as input
-->for(int i=1;i<=no;i++)

--2nd Loop will be for spaces
    consider the input as 4
    in 1st line -3 spaces are required i.e (no-1)
    2nd line -2 spaces i.e. (no-2)
    3 line-1 space i.e. (no-3)
    4 line-no space i.e. (no-4)

-->for(int j=1;j<=no-i;j++)
System.out.print(" ");// for spaces    

--3rd Loop will be for printing the symbols as per requirement:

    --for 1st line--* should be printed
    --for 2nd line-@ should be printed
    i.e. for "odd" lines-->*
    i.e. for "even" lines-->@
    --for line no 3 if no is 4 -->3 should be printed

inside this loop we wil use if-else ladder to check for above conditions as follows:

if(i==no-1)
System.out.print((no-1)+" ");
else if(i%2==0)
System.out.print("@ ");
else if(i%2==1)
System.out.print("* ");

--to move cursor to new line
Last Line of 1st outer Loop should be System.out.println();

Posted by Mânïshå Mùlchåndânï    2015-01-05 08:54:19


here we have to print a pattern in right triangular form.. here the condition is that if row no. is odd then *is prntd... if even then @ is printed and if row num is equal to (size of pattern -1) ....den (size of pttrn -1) is prunted in that row..... here we have to use two loops nested..... frst for every row. and next for printng values in each row... now as we can see no. of spaces in first row is equal to size-1 and this keeps on decremnting by 1 at each row...... so lets strt with outer loop..... take a= 1 to size..... den take another loop for printng spaces.... take b=size-1 to a..... print spaces...... den exit this inner loop. then take anothr loop for prntng charactrs.... take c=1 to a..... and den chek  if a==size -1 den print a and single space..... else if a%2==0 den print @ and singl space...  else if a is odd den print * and singl space.... exit loop chang line.. exit outr loop

Posted by Asma Mujtaba Khan    2015-01-05 12:13:08


This dose is now closed and the winners are Mânïshå Mùlchåndânï,  for 'First Correct Comment'. The 'lucky liker' is Shubham 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-06 06:06:22


its a humble request from merit campus to please tell me the mistake in my logic so that I can correct it and not repeat in future......

Posted by Asma Mujtaba Khan    2015-01-06 12:57:04


© meritcampus 2019

All Rights Reserved.

Open In App