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
Shreyas (not verified)
Tuesday, November 22, 2016 - 11:18am
Permalink
How to print this
How to print this
5
54
543
5432
54321
Rishi (not verified)
Sunday, November 27, 2016 - 5:52am
Permalink
How to print
How to print
1
12
123
1234
AJ (not verified)
Wednesday, December 21, 2016 - 12:48pm
Permalink
1 21 321 4321
1
21
321
4321
Shubham Nishad (not verified)
Thursday, April 20, 2017 - 5:09pm
Permalink
how to print this pattern
how to print this pattern
1 1
12 21
123 321
1234321
aswitha (not verified)
Monday, July 3, 2017 - 5:05pm
Permalink
how to print this pattern in
how to print this pattern in java
1
1 2 1
1 3 3 1
1 4 6 4 1
Steven Rith (not verified)
Wednesday, August 23, 2017 - 5:13pm
Permalink
pyramid by number of star
pyramid by number of star
ex: star = 4
*
***
star = 6
*
***
star = 8
*
***
*****
ELL (not verified)
Sunday, November 12, 2017 - 1:21pm
Permalink
How to print this pattern
How to print this pattern using for loop and while loop.
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
++++++++++++++
Prajakta (not verified)
Monday, December 18, 2017 - 7:26am
Permalink
How to print this pattern in
How to print this pattern in java :-
1234
2341
3412
4123
Harshita (not verified)
Friday, July 10, 2020 - 8:35am
Permalink
Scanner s=new Scanner(System
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();
}
Hello (not verified)
Sunday, January 28, 2018 - 7:45am
Permalink
INT I,j;
INT I,j;
For(I=1;I<=4;i++)
{
For(j=1;j<=I;j++)
System.out.print(j);
System.out.println();
}
}
Arjit singh (not verified)
Monday, January 29, 2018 - 10:19pm
Permalink
*---*
*---*
**-**
*****
-***-
--*--
-----
Raunak (not verified)
Monday, February 5, 2018 - 10:17am
Permalink
How to print this :-
How to print this :-
55555
54444
54333
54322
54321
54321
5
mo (not verified)
Tuesday, July 31, 2018 - 3:08am
Permalink
how to print:
how to print:
1
22
333
4444
55555
Dale (not verified)
Friday, December 7, 2018 - 5:52am
Permalink
How to print:
How to print:
1
2 3
3 4 5
4 5 6 7
Avantika (not verified)
Thursday, May 2, 2019 - 5:01am
Permalink
How to print
How to print
12345&
1234&&
123&&&
12&&&&
1&&&&&
&&&&&&
Darshika Singh (not verified)
Saturday, May 25, 2019 - 12:00pm
Permalink
Please help me with this
Please help me with this pattern.. pllz
1
121
121
srinath (not verified)
Tuesday, February 18, 2020 - 7:04am
Permalink
if n=4
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