Please help with this pattern using while loop
Problem in printing this pattern using while loop
@abhishekpandeycsaiml21_c74cb0ea3a791122 below is the approach using for loop and I assume you know how to convert a for to while loop. Try this if you have any problem after this feel free to ask.
In Pattern Question, there is a common method which we teach in course viz, taking nsp, nst, csp, cst and row variables.
Pattern Hack: Always first Try to first print pattern by ignoring the value to be printed then accommodate your value in that pattern. For e.g.,
1
2 1 1 2
3 2 1 1 2 3
4 3 2 1 1 2 3 4
3 2 1 1 2 3
2 1 1 2
1
View it as:-
*
* * * *
* * * * * *
* * * * * * * *
* * * * * *
* * * *
*
Short Info about the variables:-
- nsp (number of spaces)-> Number of spaces in very First Line of the pattern.
- nst (number of stars)-> Number of stars in very first line of the pattern.
- csp (counter of spaces)-> counter of spaces that will print the required number of spaces and will be initialized with 1 and incremented upto nsp.
- cst (counter of stars)-> counter of spaces that will print the required number of stars and will be initialized with 1 and incremented upto nst.
- rows -> It will be initialized with 1 and will go upto the total number of rows in the pattern.
–Input N is the number of rows.Given pattern is seen as first spaces ,then numbers ,then spaces and then numbers again. Till middle row, number of numbers in one batch in a row is equal to row number. After middle row, number of numbers decreases each time by 1.The max value in the pattern is in the middle row which is (N/2 +1).
–So do the work of spaces first ,then that of numbers in decreasing order, then that of spaces and then that of numbers in increasing order.
Code:
import java.util.Scanner;
public class patternDSA {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in) ;
int n = scn.nextInt() ; // number of rows
int nsp1 = n - 1; // initializing number of spaces of 1st batch
int nsp2 = -1; // initializing number of spaces of 2nd batch
int nst = 1; // initializing number of numbers
for (int row = 1; row <= n; row++) {
int val; // starting value of numbers
if (row <= n / 2 + 1) {
val = row; // till mid value is equal to row number
} else {
val = n - row + 1; // after mid values are total rows - current row +1
}
// work for spaces
for (int csp = 1; csp <= nsp1; csp++) {
System.out.print(" ");
}
// work for numbers
for (int cst = 1; cst <= nst; cst++) {
System.out.print(val + " ");
val-- ;
}
// work for spaces
for (int csp = 1; csp <= nsp2; csp++) {
System.out.print(" ");
}
int cst = 1;
val++ ;
if (row == 1 || row == n) {
cst = 2;
}
// work for numbers
for (; cst <= nst; cst++) {
System.out.print(val + " ");
val++ ;
}
//preparation for next iteration
if (row <= n / 2) {
nsp1 -= 2;
nst++;
nsp2 += 2;
} else {
nsp1 += 2;
nst--;
nsp2 -= 2;
}
System.out.println();
}
}
}
@abhishekpandeycsaiml21_c74cb0ea3a791122 you have reopened the doubt but you haven’t updated any reply. So do you have any problem then ask me. Otherwise I am closing this doubt. since Ratings of TA get affected if the doubt is unresolved for long time. If you got any question then only reopen the doubt.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.
why have you taken val=row and val=n-row+1and how you applied if else block for next iteration of pattern please explain in detail .The explanation is too short .You have just written code without explaining all these things .So i am unable to catch it please explain in details
@abhishekpandeycsaiml21_c74cb0ea3a791122 I have taken val = row bcoz whatever the value you’re printing it depends on the current row no. …and in the second half I have taken val = n - row + 1 bcoz the second half starts from 1 again therefore to develop a relation between row and val to be printed you need to do this : and this formula is developed using Common sense and simple maths.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.