Nested for Loop

Nested for Loop
Write for loop within another for loop. Such loops are called nested loops.

Print the following output
1
12
123
1234
12345
123456
1234567
12345678
123456789


class test
{
public static void main(String [] args)
{
for(int i=1;
i<=9; i++)
{
System.out.println();
for(int j=1; j<=i; j++)
{
System.out.print(j);
}
}
System.out.println();
}
}

Print the following output
*
**
***
****
*****

class test
{
public static void main(String[] args)
{
for(int i = 5; i >= 0; i–)
{
for(int s = 1; s < i; s++)
{
System.out.print(” “);
}
for(int j = 5; j >= i; j–)
{
System.out.print(“*”);
}
System.out.println(“”);
}
}
}

Print the following output
*
* *
* * *
* * * *

class test
{
public static void main(String args[])
{
int i, j, k, sp=5;
for(i=0; i<5; i++)
{
for(k=0; k<sp; k++)
{
System.out.print(” “);
}
for(j=0; j<i; j++)
{
System.out.print(“* “);
}
System.out.println(); sp–;
}
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *