NestedForLoop

 Topic : Nested For Loop

class nestedloops

{

    public static void main()

    {

        int i,j;

        for (i=1;i<=2;i++)

        {

            for(j=1;j<=5;j=j+2)

            {

                System.out.println("i ="+i+" j ="+j);

            }

        }

    }

}

 

class nestedloops2

 {

    public static void main()

    {

        int i,j;

        for (i=14;i>=10;i=i-2)

        {

            for(j=9;j>=1;j=j-4)

            {

                System.out.println("i ="+i+" j ="+j);

            }

        }

    }

}

 Topic Nested For Loop: Pattern

/*

 * WAP to get the following output:

        *

        **

        ***

 */

 

 class floydTriangle

{

    public static void main()

    {

        int i,j;

        for (i=1;i<=3;++i)      //rows

        {

            for(j=1;j<=i;j++)   //column

            {

                System.out.print("*");

            }

            System.out.println();

        }

    }

}

/*

 * WAP to get the following output:

        ****

        ***

        **

        *

 */

 

 class InvertedTriangle

{

    public static void main()

    {

        int i,j;

        for (i=1;i<=4;++i)      //rows

        {

            for(j=4;j>=i;j--)   //column

            {

                System.out.print("*");

            }

            System.out.println();

        }

    }

}

/*

 * WAP to get the following output:

        4321

        432

        43

        4

 */

 

 class invertedNum

{

    public static void main()

    {

        int i,j;

        for (i=1;i<=4;++i)      //rows

        {

            for(j=4;j>=i;j--)   //column

            {

                System.out.print(j);

            }

            System.out.println();

        }

    }

}

 

/*

 * WAP to get the following output:

        5

        54

        543

        5432

        54321

 */

 

 class triangleNum

{

    public static void main()

    {

        int i,j;

        for (i=5;i>=1;--i)      //rows

        {

           

            for(j=5;j>=i;j--)   //column

            {

                System.out.print(j);

               

            

            }

            System.out.println();

        }

    }

}

 

/*

 * WAP to get the following output:

        5

        5   4

        5   4   3

        5   4   3   2

        5   4   3   2   1

 */

 

 class triangleNumwithSpace

{

    public static void main()

    {

        int i,j;

        for (i=5;i>=1;--i)      //rows

        {

           

            for(j=5;j>=i;j--)   //column

            {

                System.out.print(j +"\t");

               

            

            }

            System.out.println();

        }

    }

}

/*

 * WAP to get the following output:

     ****

     ****

     ****

*/

 class squareSymbol

{

    public static void main()

    {

        int i,j;

        for (i=1;i<=3;i++)      //rows

        {

            for(j=1;j<=4;j++)   //column

            {

                System.out.print("*");

            }

            System.out.println();

        }

    }

}

 

/*

 * WAP to get the following output:

5

54

543

5432

54321

*/

 

 class nestedloop4

{

    public static void main()

    {

        int i,j;

        for (i=5;i>=1;--i)      //rows

        {

            for(j=5;j>=i;--j)   //column

            {

                System.out.print(j);

            }

            System.out.println();

        }

    }

}

 

/*

 * WAP to get the following output:

    A

    AB

    ABC

    ABCD

    ABCDE

A=65 to Z=90

a=97 to z=122

*/

 

 class nestedloop5

{

    public static void main()

    {

        int i,j;

        for (i=65;i<=69;i++)      //rows

        {

            for(j=65;j<=i;j++)   //column

            {

                System.out.print((char)j);

            }

            System.out.println();

        }

    }

}

 

/*

 * WAP to accept number of lines of floyd triangle required

 * and accordingly display the output.

 * for example the user enter 5, five lines of floyd triangle are:

 *

        1

        23

        456

        78910

        1112131415

 */

import java.util.Scanner;

 class floydTriangleReq

{

    public static void main()

    {

        int i=1,j,c=i,n;

        Scanner ob=new Scanner(System.in);

        System.out.println("Enter no. of lines floyd triangle required:");

        n=ob.nextInt();

        System.out.println("_____Output______");

        for (i=1;i<=n;i++)      //rows

        {

            for(j=1;j<=i;j++)   //column

            {

                System.out.print(c);

                c++;

            }

            System.out.println();

        }

    }

}

 

/*

 * WAP to get the following output:

    10  9    8   7

    6   5   4

    3   2

    1

  

*/

 

 class nestedloop7

{

    public static void main()

    {

        int i,j,c=10;

        for (i=4;i>=1;i--)      //rows

        {

            for(j=1;j<=i;j++)   //column

            {

                System.out.print(c+"\t");

                c--;

            }

            System.out.println();

        }

    }

}

 

/*

 * WAP to get the following output:

    *

    *#

    *#*

    *#*#

    *#*#*

   

*/

 

 class nestedloop8

{

    public static void main()

    {

        int i,j;

        for (i=1;i<=5;i++)      //rows

        {

            for(j=1;j<=i;j++)   //column

            {

                if(j%2==0)

                {

                    System.out.print("#");

                }

                else

                {

                    System.out.print("*");

                }

               

            }

            System.out.println();

        }

       

    }

}

 

 

/*

 * WAP to get the following output:

        1   

        1    2   

        1    2    3   

        1    2    3    4   

        1    2    3    4    5   

 

 */

 

 class tri_num_invert

 

{

    public static void main()

    {

        int i,j;

        for (i=1;i<=5;i++)      //rows

        {

           

            for(j=1;j<=i;j++)   //column

            {

                System.out.print(j +"\t");

               

            

            }

            System.out.println();

        }

    }                                                                                                                       

}

 


No comments:

Post a Comment