Menu
Topics Index
...
`

Print Number And 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 number and symbol formation depending upon input size. Use System.out.println or System.out.print for printing.

Input (Integer) Printed Output
4
1 2 
* * * 
1 2 3 4
5
1 2 
* * * 
1 2 3 4 
* * * * *
6
1 2 
* * * 
1 2 3 4 
* * * * *
1 2 3 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.

We have to use nested for loops to print the pattern... If the row number is odd try to print that number of stars and when the row is even print numbers from 1 to that row number....

Posted by Gopinath Manchikanti    2014-12-03 05:56:08


here we have to print pattern in left truangular form.....here we can see that when the row no.  is odd * is prntd and wen it is even digits are printed....no. of stars or digit in a row to b printed is equal to th row number. i. e in frst row one star is printed and so on... here to print this pattern we will use the concept of nested loop i.e loop inside loop.... frstly the outer loop will be for row count amd inner loop wil b for printing values in a particular loop.. hence overall two loops are used for this pattern...  so to print the pattern...frstly we will take a loop i=1to size... this loop is taken for rows.. now inside this loop we will take another loop j=1 to i.we r incrementung j loop till i's... bcz here we can see in each row no. of characters printed is equal to row number.... now we will have to chek whether row is odd or even to print star and digits respectively.... so we will chek i%2==0 if yes then print j otherwise print * ....by this we will get the desired pattern

Posted by Asma Mujtaba Khan    2014-12-03 07:11:36


Here we need 3 loops, 1st loop to rotate no of rows i.e i = 1 to given Size, after entered into the loop we have to check " i " is even or odd, if " i " value is odd then you need to print stmt ("*"+" ") up to " i " times using another loop, if " i " value is even then we need to print values using var " k " from k = 1 to k <= i ,and concatenate the space for to get given shape. ThanQ.

Posted by Damu Surya    2014-12-03 07:13:44


Here we can use nested for loops i.e., placing one for loop in the body of another for loop is nested loop. so here we use nested for loop. inside nested for loop we use if condition with i modulous of 2 is equal to 1. i.e., i%2==1 if this condition satisfies it will prints '*' else it will prints the values.

Posted by Sai Ram    2014-12-03 07:38:09


to implementing this program we need to use nested loop concept.

steps to follow:

1.create for loop which is use to indicate rows.
initialiaze one local variable i=1,thn give condition i>=givn input number,thn give increment operator(increment 1 by 1)

2.inner for loop which is used to print column values.
initialiaze one local variable j=1,thn give condition j>=i,thn give increment operator(increment 1 by 1)

3.here you need check outer for loop's i value if it is odd or even

4.if i is odd thn print "*" upto inner for loop's condition failed.

5.if i is even thn print inner for loop j's value .

6.if condition of outer for loop get failed thn control goes out of loop.

7.here printformat function returns nothing bcz return type is void.

Posted by Maheshwari Natarajan    2014-12-03 07:46:51


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...

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

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 k<=size-i..here i will be row number k must start from 1

Posted by Uday Kumar    2014-12-04 02:30:19


This dose is now closed and the winners are Asma Mujtaba Khan,  for 'First Correct Comment', Damodar Ukkadala, Uday Kumar,  for 'Second Correct Comment'. The 'lucky liker' is Damodar Ukkadala. 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-05 07:16:27


© meritcampus 2019

All Rights Reserved.

Open In App