Take N (number of rows), print the following pattern (for N = 4).
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
Take N (number of rows), print the following pattern (for N = 4).
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
hello @dipeshpandey2001
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 3 2
3 4 5 4 3
4 5 6 7 6 5 4
View it as:-
*
* * *
* * * * *
* * * * * * *
Given pattern is seen as first spaces and then numbers.The numbers are first in increasing order then in decreasing order. Spaces are in decreasing order. So,first do the work for spaces and then for numbers .And then update variables accordingly for next iterations.
for code refer this->
let me know if anything is not clear
val=val-2; for(col=1;col<=row-1;col++) { cout<<val<<"\t"; val–; } ,can you pls explain this part
the loop above this line prints the increasing part.
after that we are doing val-2 to get the starting of decreasing series.
and then this for loop is printing val and decrementing it by 1 . In total it is running row-1 times becuase we have row -1 elements in decreasing series .
try to dry run it on paper for clarity
for(col=1;col<=row-1;col++) still not getting this part
consider this 4th row.
here increasing part is [4,5,6,7] which already get printed by first loop.
after this val will be 8.
but our decreasing series starts from 6 (val - 2), thats why we subtracted 2.
now val is 6.
now becuase row is 4 , row -1 will be 3. so this loop will run 3 times.
on first iteration it will print 6 and decrease val by 1 so new val is 5.
on second iteration it will print 5 and decrease val by 1 so new val is 4.
on third iteration it will print 4 and decrease val by 1 so new val is 3.
and loop will end here
so finally we will get the decreasing series [6,5,4]
if i start with col=1 in this loop ,that means new coloumn i[0] will start after 7?? or it will as it was in begining…am not able to dry run this program
yes it will start after seven.
col is just a variable name that we are using to iterate row-1 times thats it. there is no other usage
okay thanks …
#include using namespace std; int main() { int i,j,n,val; cin>>n; for(i=1;i<=n;i++) { for(j=1;j<n-i;j++) { cout<<" “<<”\t"; } val=i; for(j=1;j<=i;j++) { cout<<val<<"\t"; val++; } val=val-2; for(j=1;j<=i-1;j++) { cout<<val<<"\t"; val–; } cout<<endl; } }
can you pls check the whats wrong with the code ,theres problem in 3rd line
got the problem… thanks
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.