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!
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(""); } } }
1 12 123 1234 12345 123456 123456 12345 1234 123 12 1 1 21 321 4321 54321 654321 123456 12345 1234 123 12 1
Once again, nothing is impossible, this example shows it.
Great job, you made it!
Comments
eeve (not verified)
Thursday, June 27, 2013 - 8:53am
Permalink
wat about this pattern:
wat about this pattern:
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13
nagaraju chandu (not verified)
Friday, May 29, 2015 - 5:43pm
Permalink
class Pattern
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(" ");
}
}
}
Dhrumil (not verified)
Friday, April 1, 2016 - 7:22pm
Permalink
Wat about this pattern
Wat about this pattern
654
32
1
Anonymous (not verified)
Sunday, September 22, 2019 - 8:45am
Permalink
5 45 345 2345 12345
5
45
345
2345
12345
AHD (not verified)
Sunday, June 19, 2016 - 4:42pm
Permalink
class Pattern2
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();
}
}
}
jugaduAdmi (not verified)
Wednesday, August 30, 2017 - 3:07pm
Permalink
public class Parttern
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(" ");
}
}
}
madhan minho (not verified)
Tuesday, November 4, 2014 - 7:19am
Permalink
eeve try this code
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);
}
}
suman sahu (not verified)
Friday, April 24, 2015 - 8:58am
Permalink
How to print this pattern
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
ameer (not verified)
Sunday, September 17, 2017 - 12:03pm
Permalink
use this code i hope it help
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()
}
baijuchandran (not verified)
Friday, June 12, 2015 - 10:51am
Permalink
how to print that pattern
how to print that pattern
1
212
32123
4321234
543212345
RAMA SWAMY (not verified)
Monday, November 9, 2015 - 5:49pm
Permalink
package loops;
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();
}
}
}
suganya (not verified)
Wednesday, August 5, 2015 - 5:56am
Permalink
1
1
3 2
6 5 4
10 9 8 7
Satdhruti (not verified)
Tuesday, March 8, 2016 - 4:04pm
Permalink
Hope you'll read this:
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();
}
}
}
Alankruth (not verified)
Thursday, December 29, 2016 - 7:46pm
Permalink
for(i=0;i<n;i++)
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("");
}
Ravi (not verified)
Monday, August 31, 2015 - 2:45pm
Permalink
how to print
how to print
1234
5123
4512
3451
shivam rajput (not verified)
Thursday, September 10, 2015 - 10:20am
Permalink
1 2 3 4 5
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
Umair (not verified)
Saturday, November 7, 2015 - 3:05pm
Permalink
*2345
*2345
1*345
12*45
123*5
1234*
any one write this code pllzzz?????
ABHISHEK KUMAR (not verified)
Saturday, May 14, 2016 - 1:48pm
Permalink
class looping
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);
}
}
}
}
}
ABDUL ANAM ANSARI (not verified)
Thursday, August 4, 2016 - 8:00pm
Permalink
public class Pattern4 {
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");
}
}
}
BALA (not verified)
Friday, February 3, 2017 - 2:49pm
Permalink
n=5;
n=5;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i==j)
print("*");
else
print(j);
}
print(" ");
}
Sudhir Jangir (not verified)
Sunday, November 8, 2015 - 4:08am
Permalink
1
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
sivangi (not verified)
Saturday, November 14, 2015 - 11:58am
Permalink
q1> print 1,1,2,3,5,8,13,..
q1> print 1,1,2,3,5,8,13,....till 20 terms using for loop
q2 > *****
***
*
q3> *****
***
*
q4 > *
***
*****
***
*
Harsh (not verified)
Thursday, November 26, 2015 - 2:42pm
Permalink
how to print.................
how to print..................
AAAAA
ABBBB
ABCCC
ABCDD
ABCDE
Aabir (not verified)
Wednesday, January 13, 2016 - 4:30pm
Permalink
How to print the following
How to print the following pattern............
1 2 3
4 5 6
7 8 9
siddhi (not verified)
Wednesday, March 23, 2016 - 7:29am
Permalink
what about this pattern
what about this pattern
1
21
321
4321
Aparna (not verified)
Monday, March 28, 2016 - 7:31am
Permalink
Please Help me with this.
Please Help me with this.
A B C
A B C
A B C
vaishu&hema (not verified)
Monday, July 4, 2016 - 12:07pm
Permalink
public class Demo8
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();
}
}
}
omar (not verified)
Thursday, March 31, 2016 - 8:03am
Permalink
please help me with this
please help me with this using for loop only
*******
* *
* *
* *
*******
Mairaj (not verified)
Monday, April 11, 2016 - 10:47am
Permalink
how to print this series
how to print this series
A
AL
ALI
using loops
how to print (not verified)
Wednesday, May 4, 2016 - 4:22pm
Permalink
1234567 54321 12345 321 12 1
1234567
54321
12345
321
12
1
gamer (not verified)
Sunday, May 15, 2016 - 1:00pm
Permalink
how to print:
how to print:
123454321
1234 4321
123 321
12 21
1 1
12 21
123 321
1234 4321
123454321
Disha (not verified)
Tuesday, May 24, 2016 - 5:10pm
Permalink
What about this ?
What about this ?
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Tessy george (not verified)
Thursday, August 18, 2016 - 5:11pm
Permalink
Please sent me the code of
Please sent me the code of above pattern
varsha (not verified)
Thursday, May 26, 2016 - 6:38pm
Permalink
solution for pattern:
solution for pattern:
1
12
123
1234
12345
gandharva (not verified)
Monday, September 12, 2016 - 9:26am
Permalink
class series
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();
}}}
Chaitu (not verified)
Monday, August 8, 2016 - 8:49am
Permalink
1234*
1234*
123*5
12*45
1*345
*2345
how to print the series??
priyanshu (not verified)
Wednesday, August 10, 2016 - 11:01am
Permalink
1 3 5 7 9
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
ankit (not verified)
Thursday, August 18, 2016 - 5:20pm
Permalink
1 121 12121 1212121
1
121
12121
1212121
RAHUL_DADA & VA... (not verified)
Tuesday, December 17, 2019 - 10:04am
Permalink
public static void main
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();
}
}
Mika (not verified)
Thursday, August 25, 2016 - 6:39am
Permalink
Could you please show the
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!!
gandharva (not verified)
Monday, September 12, 2016 - 9:28am
Permalink
plz solve the following
plz solve the following series
1
11
111
1111
11111
Shubham Nishad (not verified)
Thursday, April 20, 2017 - 5:15pm
Permalink
class abc
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();
}
}
}
Filomeno Giovetti (not verified)
Wednesday, September 14, 2016 - 1:03am
Permalink
what about
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
Daniyal Dany (not verified)
Thursday, September 15, 2016 - 10:26am
Permalink
HOW TO PRINT???
HOW TO PRINT???
1******
12*****
123****
1234***
12345**
123456*
1234567
Ishani (not verified)
Friday, September 16, 2016 - 11:41am
Permalink
Plz help.-
Plz help.-
A
BB
CCC
DDDD
EEEEE
DDDD
EEEEE
lady (not verified)
Sunday, October 2, 2016 - 8:50am
Permalink
How about this 1 121 12421
How about this
1
121
12421
meme (not verified)
Sunday, October 2, 2016 - 8:56am
Permalink
1
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
ashu (not verified)
Thursday, October 6, 2016 - 8:37am
Permalink
how to print
how to print
1
01
101
0101
10101
Rahul (not verified)
Friday, October 7, 2016 - 4:06pm
Permalink
1
1
12
123
1234
12345
AJ (not verified)
Wednesday, December 21, 2016 - 12:53pm
Permalink
import java.io.*;
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