Java - Tips'n Tricks - Printing four patterns using loops

In this Java tutorial we are going to see how to display four patterns using the while and for loops.

Notice that this example works in every language as well, just change the print() method depending on yours.

It seems easy, but it demands a certain reflection before finding the solution.

Of course if you are a beginner, don't be afraid, practising is the key of everything!

Code

public class PrintingFourPatternUsingLoops {

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        /**
         * First with for
         */
        for (int i = 1; i < 7; i++) {
            for (int j = 1; j < i + 1; j++) {
                System.out.print(j);
            }
            System.out.println();
        }
        
        /**
         * Second with for
         */
        System.out.println();
        for(int k = 8; k > 1; k--) {
            for(int l = 1; l < k - 1; l++){
                System.out.print(l);
            }
            System.out.println();
        }

        
        /**
         * Third with while
         */
        int i = 1;
        int j = 1;
        int k = 1;
        int max = 7;

        while (i < max) {
            k = 1;
            while (k < max - i) {
                System.out.print(' ');
                ++k;
            }
            while (j > 0) {
                System.out.print(max - (max - j));
                --j;
            }
            ++i;
            j+=i;
            System.out.println("");
        }
        System.out.println("");

        /**
         * Fourth with while
         */
        i = 1;
        j = 1;
        k = 1;
        max = 7;
        int max2 = 8;
        int tmp = 0;

        while (i < max) {
            k = 1;
            while (k < max - (max - i)) {
                System.out.print(' ');
                ++k;
            }
            tmp = max2 - i;
            j = 1;
            while (j < tmp) {
                System.out.print(max - (max - j));
                ++j;
            }
            ++i;
            System.out.println("");
        }
    }
}

Output

1
12
123
1234
12345
123456

123456
12345
1234
123
12
1

     1
    21
   321
  4321
 54321
654321

123456
 12345
  1234
   123
    12
     1

Finally

Once again, nothing is impossible, this example shows it.
Great job, you made it! devil
 

Comments

Comment: 

How to print this
5
54
543
5432
54321

Comment: 

How to print
1
12
123
1234

Comment: 

1
21
321
4321

Comment: 

how to print this pattern
1 1
12 21
123 321
1234321

Comment: 

how to print this pattern in java

1
1 2 1
1 3 3 1
1 4 6 4 1

Comment: 

pyramid by number of star
ex: star = 4
*
***
star = 6
*
***
star = 8
*
***
*****

Comment: 

How to print this pattern using for loop and while loop.

+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
++++++++++++++

Comment: 

How to print this pattern in java :-
1234
2341
3412
4123

Comment: 

Scanner s=new Scanner(System.in);
int n=s.nextInt();
int t=0;

for(int i=1;i<=n;i++)
{ int count=1;
t=i;
if (i>4)
{
t=i-4;
}

for(int j=t;j<=4;j++)
{
System.out.print(j + " ");
count++;

}
int k=1;

while(count<=4)
{
System.out.print(k + " ");
k++;
count++;

}

System.out.println();
}

Comment: 

INT I,j;
For(I=1;I<=4;i++)
{
For(j=1;j<=I;j++)
System.out.print(j);
System.out.println();
}
}

Comment: 

*---*
**-**
*****
-***-
--*--
-----

Comment: 

How to print this :-
55555
54444
54333
54322
54321
54321
5

Comment: 

how to print:

1
22
333
4444
55555

Comment: 

How to print:
1
2 3
3 4 5
4 5 6 7

Comment: 

How to print
12345&
1234&&
123&&&
12&&&&
1&&&&&
&&&&&&

Comment: 

Please help me with this pattern.. pllz
1
121
121

Comment: 

if n=4

4 8 16 32
2 4 8 16
2 4 8 16
4 8 16 32

if n=5

5 10 20 40
2 4 8 16
2 6 12 24
4 8 16 32
1 2 4 8

if n=6

6 12 24 48
2 4 8 16
4 8 16 32
4 8 16 32
2 4 8 16
6 12 24 48

can any solve this

Pages

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.