Menu
Topics Index
...
`

Printed output format


If you need explanation Read this topic

If you need Answer Take test on this topic

Printed output format
What will be the output of the following program?
class PrintFormat
{
    public static void main(String[] args)
    {
        for (int i = 0; i < 5; i++)
        {
            for (int j = 5; j >= i; j--)
            {
                System.out.print(j);
            }
            System.out.println();
        }
    }
}

A. 01234
1234
234
34
4
B. 01234
01234
01234
01234
01234
C. 543210
54321
5432
543
54
D. 54321
5432
543
54
5
E. Compilation Error
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.

C

Posted by Harish Kumar G    2015-02-02 03:49:16


ans c.here nested for loop is used, for i=0 ,inner for loop prints numbers from 5 to 0 in decrease order.similarly for i =1 ,innr for loop prints 5 to 1, inner for loop iteration depends on i value,it prints upto i value ,

Posted by Maheshwari Natarajan    2015-02-02 03:59:39


Ans is C.
Here we are using nested for loop in which the inner for loop will print numbers from 5 to 0 in decreasing order where as the outer for loop will execute inner for loop from (0 to 4) < 5... Inner for loop depends on outer for loop variable i, it prints upto the value of i from j.

Posted by Gopinath Manchikanti    2015-02-02 10:32:08


c

Posted by Shubham Bansal    2015-02-02 11:44:01


Ans is Option C... Here Execution starts from main() method here we are taking 2 for-loops first for-loop contains range from i=0 to i<5 and i++ that means it will execute till i=4 and from 0-4 means 5 rows will print inside this for-loop we are taking another for-loop with the range j=5 to j>=i and j--  this loop will act as number of digits in  a  particular row                                               for the first iteration i value is 0 and it is <5 so it will enter into inner loop here j=5 and 5>=0 and j-- so condition is true and inside this loop we are printing the values of j and we are printing using print statement so it will print in a single row till 543210 after that System.out.println() will execute due to this statement compiler will go to next line after that i value will increase by 1 now i=1 1<5 condition is true it will enter into inner loop now j=5 and j>=1 j-- that means it will print from 5 to 1 like 54321 like this it will execute 5 rows from i=0 to i=4 so the Ans is Option C                                                    543210                                                                                                                 54321                                                                                                                          5432                                                                                                                      543                                                                                                               54

Posted by Bhagi Bhagyasri    2015-02-02 15:03:56


ANS IS C

--Here,the concept of "NESTED LOOPS" are used.

--Nested loops means one "FOR LOOP i.e. INNER LOOP" is written inside another "FOR LOOP i.e. OUTER LOOP".

--Outer loop runs from 0 to 4.For each iteration from 0 to 4 inner loop counter runs.Inner loop runs from 5 till counter of outer loop and get decremented by 1.

--1st iteartion i=0
j=5,j>=0(true)
5
j-- i.e. j=4
j=4,j>=0(true)
4
j-- i.e. j=3
j=3,j>=0(true)
3
j-- i.e. j=2
j=2,j>=0(true)
2
j-- i.e. j=1
j=1,j>=0(true)
1
j-- i.e. j=0
j=0,j>=0(true)
0

--Same is applied for each iteration
for i=1
5 4 3 2 1
for i=2
5 4 3 2
for i=3
5 4 3
for i=4
5 4

Posted by Mânïshå Mùlchåndânï    2015-02-02 17:11:47


This dose is now closed and the winners are Maheshwari Natarajan,  for 'First Correct Comment', Bhagi Bhagyasri, Mânïshå Mùlchåndânï,  for 'Best Comment' and Mânïshå Mùlchåndânï for the 'Popular Comment'. The 'lucky liker' is ?????????? ?????. 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-03 02:12:17


© meritcampus 2019

All Rights Reserved.

Open In App