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

Post a Comment

POST Answer of Questions and ASK to Doubt

Popular posts from this blog

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...

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...

JDBC using JSP and Servlet

JDBC means Java Database Connectivity ,It is intermediates from Application to database. JDBC has different type of divers and provides to communicate from database server. JDBC contain four different type of approach to communicate with Database Type 1:- JDBC-ODBC Driver Type2:- JDBC Vendor specific Type3 :- JDBC Network Specific Type4:- JDBC Client-Server based Driver  or JAVA thin driver:- Mostly we prefer Type 4 type of Driver to communicate with database server. Step for JDBC:- 1  Create Database using MYSQL ,ORACLE ,MS-SQL or any other database 2   Create Table using database server 3   Create Form according to database table 4  Submit Form and get form data into servlet 5  write JDBC Code:-     5.1)   import package    import java.sql.*     5.2)  Add JDBC Driver according to database ide tools     5.3)  call driver in program         ...