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: 

wat about this pattern:
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13

Comment: 

class Pattern
{
public static void main(String[]args)
{
int num=13;
for(int i=1;i<=13;i++)
{
for(int j=;j<=4;j++)
{
System.out.print(i+" ");
i++;
}
System.out.println(" ");
}
}
}

Comment: 

Wat about this pattern
654
32
1

Comment: 

5
45
345
2345
12345

Comment: 

class Pattern2
{
public static void main(String args[])
{
int q=1;
for(int i=1;i<=4;i++)
{
if(i%2!=0)
{
for(int j=q;j<=q+3;j++)
{
System.out.print(j);
}
}
else{
for(int j=q+3;j>=q;j--)
{
System.out.print(j);
}

}q+=4;
System.out.println();
}
}
}

Comment: 

public class Parttern
{

public static void main(String[]args)
{
int num=1;
int num1=8;
int num2=9;
int num3=16;
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
if(i==1)
{
System.out.print(num+" ");
num=num+1;
}
else if(i==2)
{

System.out.print(num1+" ");
num1=num1-1;
}
else if(i==3)
{

System.out.print(num2+" ");
num2=num2+1;
}
else if(i==4)
{

System.out.print(num3+" ");
num3=num3-1;
}
}
System.out.println(" ");
}
}

}

Comment: 

eeve try this code
public class Pattern
{
public static void main(String[] ar)
{
String x="";
for(int i=1;i<17;i++)
{
if(i<=12)
{
System.out.print(i);
}
else
{
x=i+""+x;
}
if(i%4==0&&i<=12)
System.out.println();
}
System.out.println(x);
}
}

Comment: 

How to print this pattern using for loop and please explain the steps using comment lines
123454321
1234_4321
123___321
12_____21
1_______1

Comment: 

use this code i hope it help you
these are kotlin code

for(i in 5 downTo 1)
{
for (j in 1..i)
{
print(j)
}
for(k in i..4)
{
print("__")
}
for (l in i downTo 1)
{
print(l)
}
println()

}

Comment: 

how to print that pattern

1
212
32123
4321234
543212345

Comment: 

package loops;

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

}

}

Comment: 

1
3 2
6 5 4
10 9 8 7

Comment: 

Hope you'll read this:

import java.io.*;
public class Test_Patterns
{
public static void main(String args[])throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter number of lines:");
int n = Integer.parseInt(br.readLine());
for(int i = 1, a = 0, b = 0; i <= n; i++){
a += i;
b = a;
for(int j = b; j > (b - i); j--)
System.out.print(j + " ");
System.out.println();
}
}
}

Comment: 

for(i=0;i<n;i++)
{
t=w+lc;
for(j=0;j<lc;j++)
{
System.out.print(--t+"\t");
w++;
}
lc++;
System.out.println("");
}

Comment: 

how to print
1234
5123
4512
3451

Comment: 

1 2 3 4 5
11 12 13 14 15
21 22 23 24 25
16 17 18 19 20
6 7 8 9 10
If n=5

Comment: 

*2345
1*345
12*45
123*5
1234*
any one write this code pllzzz?????

Comment: 

class looping
{
public Static void main (String args[])
{
for (int i=1;i<=5;i++)
{
for (int j=1;j<=5;j++)
{
if(i==j)
{
System.out.println("*");
}
else
{
System.out.println(j);
}
}
}
}
}

Comment: 

public class Pattern4 {
public static void main(String[] args) {
//i m writing only concept
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
if(i==1&j==1||i==2&&j==2||i==3&&j==3||i==4&&j==4||i==5&&j==5)
{
System.out.print("*");
}
else
{
System.out.print(j);
}
}
System.out.print("\n");
}
}

}

Comment: 

n=5;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i==j)
print("*");
else
print(j);
}
print(" ");
}

Comment: 

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

Comment: 

q1> print 1,1,2,3,5,8,13,....till 20 terms using for loop
q2 > *****
***
*
q3> *****
***
*
q4 > *
***
*****
***
*

Comment: 

how to print..................
AAAAA
ABBBB
ABCCC
ABCDD
ABCDE

Comment: 

How to print the following pattern............
1 2 3
4 5 6
7 8 9

Comment: 

what about this pattern
1
21
321
4321

Comment: 

Please Help me with this.

A B C
A B C
A B C

Comment: 

public class Demo8
{
public static void main(String[] args)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<1;j++)
{
System.out.print("A");

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

}
for(int l=0;l<1;l++)
{
System.out.print("C");

}
System.out.println();
}

}

}

Comment: 

please help me with this using for loop only
*******
* *
* *
* *
*******

Comment: 

how to print this series
A
AL
ALI
using loops

Comment: 

1234567
54321
12345
321
12
1

Comment: 

how to print:
123454321
1234 4321
123 321
12 21
1 1
12 21
123 321
1234 4321
123454321

Comment: 

What about this ?
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Comment: 

Please sent me the code of above pattern

Comment: 

solution for pattern:
1
12
123
1234
12345

Comment: 

class series
{
static void main()
{
for(int x=1;x<=5;x++){
for(int y=1;y<=x;y++){
System.out.print(y);
}
System.out.println();
}}}

Comment: 

1234*
123*5
12*45
1*345
*2345
how to print the series??

Comment: 

1 3 5 7 9
3 5 7 9 1
5 7 9 1 3
7 9 1 3 5
9 1 3 5 7

Comment: 

1
121
12121
1212121

Comment: 

public static void main(String[] args) {
int arr[]= {1,2,1,2,1};
int z=1;
for(int i=1; i<4; i++) {
for(int j=2; j>=i; j--) {
System.out.print(" ");
}
for(int k=0; k<z; k++) {
System.out.print(arr[k]);
}
z=z+2;
System.out.println();
}
}

Comment: 

Could you please show the code for
5
44
333
2222
11111
And if possible, please tell me what's the logic behind these loops and what you need to do to get what.
I mean to ask: could you please inform me about the explanation behind every game with 'i' and 'j' loops.
I'd be deeply grateful!!

Comment: 

plz solve the following series
1
11
111
1111
11111

Comment: 

class abc
{
public static void main(String args[])
{
for(int x=1;x<=5;x++)
{
for(int y=1;y<=x;y++)
{
System.out.print("1");
}
System.out.println();
}
}
}

Comment: 

what about
A1 B1 C1 - D1 E1 F1 G1 - H1 I1 J1K1
B2 C2 - D2 E2 F2 G2 - H2 I2 J2
C3 - D3 E3 F3 G3 - H3 I3
D4 E4 F4 G4 - H4

Comment: 

HOW TO PRINT???

1******
12*****
123****
1234***
12345**
123456*
1234567

Comment: 

Plz help.-
A
BB
CCC
DDDD
EEEEE
DDDD
EEEEE

Comment: 

How about this
1
121
12421

Comment: 

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

Comment: 

how to print
1
01
101
0101
10101

Comment: 

1
12
123
1234
12345

Comment: 

import java.io.*;
class P
{
public static void main(String args[])
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println();
}
}
}

Pages

Add new comment

Plain text

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