Skip to main content

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

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

Comments

  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
  15. /*
    1 2 3 4 5
    1 2 3 4
    1 2 3
    1 2
    1
    */class NestedLoop2
    {
    public static void main(String[] args) {
    int i=1,j;
    for(i=5;i>=1;i--)
    {
    for(j=1; j<=i;j++)
    {
    System.out.print(j+" ");

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

    ReplyDelete
  16. /*
    A a B b C
    A a B b
    A a B
    A a
    A
    */
    class ABCDE2
    {
    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();


    }
    }


    }

    ReplyDelete
  17. /*
    1 2 3 4 5
    5 4 3 2
    1 2 3
    5 4
    1*/

    class Pattern
    {

    public static void main(String[] args) {

    int i,j;
    for(i=1;i<=5;i++)
    {
    for(j=1;j<=6-i;j++)
    {
    if(i%2!=0)
    System.out.print(j+" ");
    else
    System.out.print(6-j+" ");


    }
    System.out.println();
    }

    }
    }

    ReplyDelete
  18. // ADITYA BAGHEL

    // WAP to print the following statement?

    // 1 2 3 4 5
    // 1 2 3 4
    // 1 2 3
    // 1 2
    // 1

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

    ReplyDelete
  19. // ADITYA BAGHEL

    // WAP to print the following statement?

    // A a B b C
    // A a B b
    // A a B
    // A a
    // A

    class Loop
    {
    static public void main(String ar[])
    {
    char up='A';
    char down='a';
    for(int i=5;i>=1;i--)
    {
    for(int j=1;j<=i;j++)
    {
    if(j%2==0)
    System.out.print(down+++" ");
    else
    System.out.print(up+++" ");

    }
    up='A';
    down='a';
    System.out.println();
    }
    }
    }

    ReplyDelete
  20. // ADITYA BAGHEL

    // WAP to print the following statement?

    // 1 0 0 1 0
    // 1 0 0 1
    // 1 0 0
    // 1 0
    // 1

    class Loop
    {
    static public void main(String ar[])
    {
    for(int i=5;i>=1;i--)
    {
    for(int j=1;j<=i;j++)
    {
    if(j==2 || j==3 || j==5)
    System.out.print(0+" ");
    else
    System.out.print(1+" ");
    }
    System.out.println();
    }


    }
    }

    ReplyDelete
  21. // ADITYA BAGHEL

    // WAP to print the following statement?

    // A B C D E
    // B C D E
    // C D E
    // D E
    // E

    class Loop
    {
    static public void main(String ar[])
    {
    char up='A';
    char rank='A';

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

    }

    }


    }

    ReplyDelete
  22. /*
    A B C D E
    B C D E
    C D E
    D E
    E
    */



    class Pattern6
    {
    public static void main(String...args)
    {
    for(int i=0;i<5;i++)
    {
    for(int k=0;k<i;k++)
    System.out.print(" ");
    for(int j=i;j<5;j++)
    {
    System.out.print((char)(65+j));
    }
    System.out.println();
    }

    }
    }

    ReplyDelete
  23. /*
    A B C D E
    B C D E
    C D E
    D E
    E
    */



    class Pattern6
    {
    public static void main(String...args)
    {
    for(int i=0;i<5;i++)
    {
    for(int k=0;k<i;k++)
    System.out.print(" ");
    for(int j=i;j<5;j++)
    {
    System.out.print((char)(65+j));
    }
    System.out.println();
    }

    }
    }

    ReplyDelete
  24. // ADITYA BAGHEL

    // WAP to print the following statement?

    // 8
    // 8 8 8
    // 8 8 8 8 8
    // 8 8 8
    // 8
    class Loop
    {
    static public void main(String ar[])
    {
    int x=0;

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

    }
    }

    ReplyDelete
  25. A B C D E
    B C D E
    C D E
    D E
    E
    .............................................................

    class Pattern
    {
    public static void main(String args[])
    {
    int i,j,k;
    int l=0;
    for(i=1;i<=5;i++)
    {
    char ch=65;
    for(k=1;k<=i-1;k++)
    {
    System.out.print(" ");
    }
    for(j=i;j<=5;j++)
    {
    System.out.print((char)(ch+l)+"");
    ch++;
    }
    System.out.println();
    l++;
    }
    }
    }

    ReplyDelete
  26. 8
    888
    88888
    888
    8


    class Star{
    public static void main(String args[])
    {
    int i,j,k;
    for(i=1;i<=3;i++)
    {
    for(k=1;k<(5-i-1);k++)
    {
    System.out.print(" ");}
    for(j=1;j<=(2*i-1);j++)
    {System.out.print("8");}
    System.out.println();
    }
    for(i=1;i<=2;i++)
    {
    for(k=1;k<=i;k++)
    {
    System.out.print(" ");}
    for(j=i;j<=(4-i);j++)
    {System.out.print("8");}
    System.out.println();
    }


    }
    }

    ReplyDelete
  27. //8
    //888
    //88888
    //888
    //8


    class Eight
    {
    public static void main(String args[])
    {
    int i,j,k;
    for(i=1;i<=3;i++)
    {
    for(k=1;k<(5-i-1);k++)
    {
    System.out.print(" ");}
    for(j=1;j<=(2*i-1);j++)
    {System.out.print("8");}
    System.out.println();
    }
    for(i=1;i<=2;i++)
    {
    for(k=1;k<=i;k++)
    {
    System.out.print(" ");}
    for(j=i;j<=(4-i);j++)
    {System.out.print("8");}
    System.out.println();
    }


    }
    }

    ReplyDelete
  28. //WAP to print
    //12345
    //1234
    //123
    //12
    //1
    class Fn1
    {
    public static void main(String args[])
    {
    for(int i=1;i<6;i++)
    {
    for(int j=1;j<=6-i;j++)
    {
    System.out.print(j);
    }
    System.out.println();
    }

    }
    }

    ReplyDelete
  29. // To print
    //12345
    //5432
    //123
    //54
    //1
    class Fn2
    {
    public static void main(String args[])
    {
    for(int i=1;i<6;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();
    }
    }
    }

    ReplyDelete
  30. //To print
    //ABCDE
    // BCDE
    // CDE
    // E
    class Fn4
    {
    public static void main(String args[])
    {
    for(int i=1;i<=5;i++)
    {
    int k = 65;
    for(int j=1;j<i;j++)
    System.out.print(" ");
    for(int a=1;a<=6-i;a++)
    {
    char ch=(char)k;
    System.out.print(ch);
    k++;
    }
    System.out.println();
    }
    }
    }

    ReplyDelete
  31. //to print
    // 8
    // 888
    // 88888
    // 888
    // 8
    class Fn6
    {
    public static void main(String args[])
    {
    int i,j,k;
    for(i=1;i<=3;i++)
    {
    for(j=3;j>i;j--)
    System.out.print(" ");
    for(k=1;k<=(2*i-1);k++)
    System.out.print(8);
    System.out.println();
    }
    for(i=2;i>=1;i--)
    {
    for(j=3;j>i;j--)
    System.out.print(" ");
    for(k=1;k<=(2*i-1);k++)
    System.out.print(8);
    System.out.println();
    }
    }
    }

    ReplyDelete
  32. /*
    1 2 3 4 5
    5 4 3 2
    1 2 3
    5 4
    1
    */

    class For15
    {
    public static void main(String rgs[])
    {
    for(int i=1;i<=5;i++)
    {
    for(int j=1;j<=6-i;j++)
    {
    if(i%2==0)

    {
    System.out.print(6-j);
    }
    else
    System.out.print(j);


    }

    System.out.println();


    }


    }



    }

    ReplyDelete
  33. WAP to print the following statement?

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



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

    }

    ReplyDelete
  34. A a B b C
    A a B b
    A a B
    A a
    A


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

    }

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

    class NestedExample3
    {
    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();
    }
    }
    }

    ReplyDelete
  36. /*1 0 0 1 0
    1 0 0 1
    1 0 0
    1 0
    1*/


    class Nested1001
    {
    public static void main(String args[])
    {
    for(int i=1;i<=5;i++)
    {
    for(int j=1;j<=6-i;j++)
    {
    if(j==1 || j==4)
    System.out.print(1);
    else
    System.out.print(0);
    }
    System.out.println();
    }
    }
    }

    ReplyDelete
  37. /*
    A B C D E
    B C D E
    C D E
    D E
    E
    */

    class NestedE2A
    {
    public static void main(String args[])
    {
    int num=0;
    for(int i=65;i<=69;i++)
    {
    for(int j=i;j<=69;j++)
    {
    System.out.print((char)j);
    }
    System.out.println();
    num++;
    for(int k=1;k<=num;k++)
    {
    System.out.print(" ");
    }
    }
    }
    }

    ReplyDelete
  38. /*Dynamic Pattern , you only give number of row
    eg :- 5
    A B C D E
    B C D E
    C D E
    D E
    E
    */

    import java.util.Scanner;

    class Pattern1
    {
    public static void main(String...arg)
    {
    Scanner c = new Scanner(System.in);
    System.out.print("Enter number of row : ");
    int n=c.nextInt();

    for(int i=65;i<=(65+(n-1));i++)
    {
    for(int j=65;j<=(65+(n-1));j++)
    {
    if(i>j)
    System.out.print(" ");
    else
    System.out.print((char)j);
    }
    System.out.println();
    }

    }
    }

    ReplyDelete

Post a Comment

POST Answer of Questions and ASK to Doubt

Popular posts from this blog

DSA in C# | Data Structure and Algorithm using C#

  DSA in C# |  Data Structure and Algorithm using C#: Lecture 1: Introduction to Data Structures and Algorithms (1 Hour) 1.1 What are Data Structures? Data Structures are ways to store and organize data so it can be used efficiently. Think of data structures as containers that hold data in a specific format. Types of Data Structures: Primitive Data Structures : These are basic structures built into the language. Example: int , float , char , bool in C#. Example : csharp int age = 25;  // 'age' stores an integer value. bool isStudent = true;  // 'isStudent' stores a boolean value. Non-Primitive Data Structures : These are more complex and are built using primitive types. They are divided into: Linear : Arrays, Lists, Queues, Stacks (data is arranged in a sequence). Non-Linear : Trees, Graphs (data is connected in more complex ways). Example : // Array is a simple linear data structure int[] number...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...

Top 50 Most Asked MERN Stack Interview Questions and Answers for 2025

 Top 50 Most Asked MERN Stack Interview Questions and Answers for 2025 Now a days most of the IT Company asked NODE JS Question mostly in interview. I am creating this article to provide help to all MERN Stack developer , who is in doubt that which type of question can be asked in MERN Stack  then they can learn from this article. I am Shiva Gautam,  I have 15 Years of experience in Multiple IT Technology, I am Founder of Shiva Concept Solution Best Programming Institute with 100% Job placement guarantee. for more information visit  Shiva Concept Solution 1. What is the MERN Stack? Answer : MERN Stack is a full-stack JavaScript framework using MongoDB (database), Express.js (backend framework), React (frontend library), and Node.js (server runtime). It’s popular for building fast, scalable web apps with one language—JavaScript. 2. What is MongoDB, and why use it in MERN? Answer : MongoDB is a NoSQL database that stores data in flexible, JSON-like documents. It...