Thursday, February 6, 2014

Nested Loop - I



When a loop have one or more loop within a loop is known as nested loop,  The interior loop within a body of an external loop is known as inner loop and the external loop called as outer loop. For every pass of outer loop invoke the inner loop to complete repetitions.

Example:

for(i=1;i<=3;I++)                         // Outer loop
{
for(j=1;j<=3;j++)     //. Inner loop
{
-------
-------
}
}

Working

---------------------------------------
i                  j
.---------------------------------------
                   1                  1
                                      2
                                      3
---------------------------------------
                   2                  1
                                      2
                                      3
---------------------------------------
                   3                  1
                                      2
                                      3

At first pass, when i=1 the j loop will complete its repeatation and same when i=2 and i=3.


Source Code


import java.io.*;
    public class Pattern_1
        {
            public static void main(String args[])throws IOException
                {
                    int n;
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    System.out.print("\n\tEnter the Size. : ");
                    n=Integer.parseInt(Br.readLine());
                    Pattern_1 NP=new Pattern_1();
                    System.out.println("\n\tNumber Pattern-1 ");
                    NP.Pat_1(n);
                    System.out.println("\n\tNumber Pattern-2 ");                   
                    NP.Pat_2(n);
                    System.out.println("\n\tNumber Pattern-3 ");                      
                    NP.Pat_3(n);
                    System.out.println("\n\tCharacter Pattern ");                      
                    NP.Pat_4(n);                   
               }
            void Pat_1(int n)
                {
                    for(int i=1;i<=n;i++)
                        {
                            for(int j=1;j<=n;j++)
                                {
                                    System.out.print(i+" ");
                                }
                    System.out.println();           
                        }
                }
            void Pat_2(int n)
                {
                    for(int i=1;i<=n;i++)
                        {
                            for(int j=1;j<=i;j++)
                                {
                                    System.out.print(i+" ");
                                }
                    System.out.println();           
                        }
                }    
            void Pat_3(int n)
                {
                    for(int i=n;i>=1;i--)
                        {
                            for(int j=n;j>=i;j--)
                                {
                                    System.out.print(i+" ");
                                }
                    System.out.println();           
                        }
                } 
            void Pat_4(int n)
                {
                    for(int i=1;i<=n;i++)
                        {
                            for(int k=1;k<=n-i;k++)
                                {
                                    System.out.print(" ");
                                }
                            for(int j=1;j<i*2;j++)
                                {
                                    System.out.print("*");
                                }
                    System.out.println();           
                        }
                }
            }           




Output




          Enter the Size. : 3

          Number Pattern-1
1 1 1
2 2 2
3 3 3

          Number Pattern-2
1
2 2
3 3 3

          Number Pattern-3
3
2 2
1 1 1

          Character Pattern
  *
 ***
*****

No comments: