Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Nested For Loop in java?


Nested For Loop means we will write more than one for loop using nested sequence.it has a collection of inner for loop and outer for loop,
The outer for loop will execute once but the inner for loop will continuously be executed.

for(init;condition;increment)
{
    for(init;condition;increment)
     {
         System.out.print("statement");
    }
    System.out.println();

}

WAP to print the following statement?

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
............................................................................................

A a B b C
A a B b
A a B
A a
A
..................................................................................................


A solution to This Program:


class Pattern
{
   public static void main(String args[])
   {
     for(int i=1;i<=5;i++)
     {
         int asc=65;
         for(int j=1;j<=6-i;j++)
          {
              if(j%2!=0)
              System.out.print((char)asc+" ");
              else
              {
              System.out.print((char)(asc+32)+" ");
              asc++;
              } 

          }
         System.out.println();   
         

     }       
   }


}

............................................................................................................................................

1 2 3 4 5
5 4 3 2
1 2 3
5 4
1
.......................................................................


class Pattern
{
   public static void main(String args[])
   {
     for(int i=1;i<=5;i++)
     {
       
         for(int j=1;j<=6-i;j++)
          {
              if(i%2!=0)
              System.out.print(j+" ");
              else
              {
              System.out.print((6-j)+" ");
           
              } 

          }
         System.out.println();   
         

     }       
   }


}

.....................................................................................................................................................

ASSIGNMENT:-

1 0 0 1 0
1 0 0 1
1 0 0
1 0
1
.................................................................................

A B C D E
    B C D E
        C D E
            D E
                E
.........................................................................................

             8
     8      8     8
8   8     8      8    8
     8     8      8
            8

................................................................................................









14 Comments

If you have any doubt in programming or join online classes then you can contact us by comment .

  1. 1 2 3 4 5
    1 2 3 4
    1 2 3
    1 2
    1
    ............................................................................................

    class nestedloop
    {
    public static void main(String[]arg)
    {
    for(int i=5; i>=1; i--)
    {
    for(int j=1; j<=i; j++)
    {
    System.out.print(j);
    }
    System.out.println();
    }
    }
    }

    ReplyDelete
  2. 1 2 3 4 5
    5 4 3 2
    1 2 3
    5 4
    1
    .......................................................................

    class loop
    {
    public static void main(String[]arg)
    {
    for(int i=5; i>=1; i--)
    {
    for(int j=1; j<=i; j++)
    {
    if(i%2!=0)
    System.out.print(j);
    else
    System.out.print(6-j);

    }
    System.out.println();
    }
    }
    }

    ReplyDelete
  3. A B C D E
    B C D E
    C D E
    D E
    E
    ---------------------------------------------------------
    class loop
    {
    public static void main(String[]arg)
    {
    int c=0;
    for(int i=65; i<=69; i++)
    {
    for(int j=i; j<=69; j++)
    {
    System.out.print((char)j);
    }
    System.out.println();
    c++;

    for(int k=1; k<=c;k++)
    {
    System.out.print(" ");
    }
    }
    }
    }

    ReplyDelete
  4. public class Main
    {
    public static void main(String[] args)
    {
    int i,j;
    for(i=5;i>=1;i--)
    {
    for(j=1;j<=i;j++)
    {

    System.out.print(j);
    }

    System.out.println();
    }
    }
    }

    ReplyDelete



  5. public class Main
    {
    public static void main(String[] args)
    {
    int i,j;
    for(i=1;i<=5;i++)
    {
    for(j=1;j<=i;j++)
    {

    System.out.print(j);
    }

    System.out.println();
    }
    }

    ReplyDelete
  6. 11
    22
    333
    444
    55555
    {
    public static void main(String[] args)
    {
    int i,j;
    for(i=1;i<=5;i++)
    {
    for(j=1;j<=i;j++)
    {

    System.out.print(i);
    }

    System.out.println();
    }
    }
    }

    ReplyDelete
  7. 54321
    4321
    321
    21
    1
    public class Main
    {
    public static void main(String[] args) {
    int i,j;
    for(i=5;i>=1;i--)
    {
    for(j=i;j>=1;j--)
    {
    System.out.print(j);

    }


    System.out.println();
    }
    }
    }

    ReplyDelete
  8. 55555
    4444
    333
    22
    1

    public class Main
    {
    public static void main(String[] args) {
    int i,j;
    for(i=5;i>=1;i--)
    {
    for(j=1;j<=i;j++)
    {
    System.out.print(i);

    }


    System.out.println();
    }
    }
    }

    ReplyDelete
  9. /******************************************************************************

    Online Java Compiler.
    Code, Compile, Run and Debug java program online.
    Write your code in this editor and press "Run" button to execute it.

    *******************************************************************************/

    public class Main
    {
    public static void main(String arg[])
    {
    for(int i=1;i<=5;i++)
    {


    for(int k=5;k>=i;k--)
    {
    System.out.print(" ");
    }
    for(int j=1;j<=2*i-1;j++)
    {
    System.out.print("*");
    }



    System.out.println();
    }
    for(int i=5;i>=1;i--)
    {


    for(int k=5;k>=i;k--)
    {
    System.out.print(" ");
    }
    for(int j=1;j<=2*i-1;j++)
    {
    System.out.print("*");
    }

    System.out.println();
    }
    }
    }



    \\mayur yadav||||||||||||||||||||||||||||||||||||||||||||||||||||

    ReplyDelete
  10. /******************************************************************************//MAyur YaDAV

    Online Java Compiler.
    Code, Compile, Run and Debug java program online.
    Write your code in this editor and press "Run" button to execute it.

    *******************************************************************************/

    public class Hello
    {
    public static void main(String arg[])
    {
    for(int i=1;i<=6;i++)
    {


    for(int k=5;k>=i;k--)
    {
    System.out.print(" ");
    }
    for(int j=1;j<=2*i-1;j++)
    {
    System.out.print("\uD83D\uDC7D");

    }
    System.out.println();
    }

    for(int i=5;i>=1;i--)
    {
    for(int k=5;k>=i;k--)
    {
    System.out.print(" ");
    }
    for(int j=1;j<=2*i-1;j++)
    {
    System.out.print("\uD83D\uDC7D");

    }

    System.out.println();
    }
    }
    }

    ReplyDelete
  11. 54321
    4321
    321
    21
    1




    class Nestedloop1
    {
    public static void main(String[]arg)
    {
    for(int i=1; i<=5; i++)
    {
    for(int j=5; j>=i; j--)
    {
    System.out.print(j);
    }
    System.out.println();
    }
    }
    }

    ReplyDelete
  12. class Star1
    {
    public static void main(String[]args)
    {
    for(int i=1;i<=4;i++)
    {
    for(int j=1;j<=i;j++)
    {
    System.out.print("*");
    }
    System.out.println();
    }
    for(int i=1;i<=4;i++)
    {
    for(int j=4;j>=i;j--)
    {
    System.out.print("*");
    }
    System.out.println();
    }




    }
    }

    -------------------------------------------------------

    *
    **
    ***
    ****
    ***
    **
    *

    ReplyDelete
  13. class Star2
    {
    public static void main(String[]args)
    {
    for(int i=1;i<=4;i++)
    {
    for(int j=3;j>=i;j--)
    {
    System.out.print(" ");
    }
    for(int k=1;k<=i;k++)
    {
    System.out.print("*");
    }
    System.out.println();
    }

    for(int i=1;i<=3;i++)
    {
    for(int k=1;k<=i;k++)
    {
    System.out.print(" ");
    }
    for(int j=3;j>=i;j--)
    {
    System.out.print("*");
    }
    System.out.println();
    }




    }
    }


    -----------------------------------------------------------


    *
    **
    ***
    ****
    ***
    **
    *

    ReplyDelete
  14. class Diamond
    {
    public static void main(String[]args)
    {
    for(int i=1;i<=4;i++)
    {
    for(int j=3;j>=i;j--)
    {
    System.out.print(" ");
    }
    for(int k=1;k<=i;k++)
    {
    System.out.print("@");
    }
    for(int l=2;l<=i;l++)
    {
    System.out.print("@");
    }
    System.out.println();
    }
    for(int i=1;i<=3;i++)
    {
    for(int j=1;j<=i;j++)
    {
    System.out.print(" ");
    }
    for(int k=3;k>=i;k--)
    {
    System.out.print("@");
    }
    for(int l=2;l>=i;l--)
    {
    System.out.print("@");
    }
    System.out.println();
    }
    }
    }


    -------------------------------------------------


    *
    ***
    *****
    ***
    *

    ReplyDelete

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post