Please guide how to solve the question


This is my code and I can’t find any logical flaw in how I approached the question. Please tell me my mistake.

hi @akshat1409
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 3 2
               3 4 5 4 3
             4 5 6 7 6 5 4

View it as:-

                   * 
                 * * *
               * * * * *
             * * * * * * * 

Short Info about the variables:-

1. nsp (number of spaces)-> Number of spaces in very First Line of the pattern.
2. nst (number of stars)-> Number of stars in very first line of the pattern.
3. 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.
4. 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.
5. rows -> It will be initialized with 1 and will go upto the total number of rows in the pattern.

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.

Have a look at this code–>

https://ide.codingblocks.com/s/664874 what is wrong with this code? It is exactly the same as your but still not working

hey, apologies for delayed response.

corrected code–>

#include<iostream>
using namespace std;
int main() {
	int n;
    cin>>n;
	int nso= n-1;
	int no = 1;
	for(int i=1;i<=n;i++){
		for(int sp=1;sp<=nso;sp++){
			cout<<" "<<"\t";
		}
		for(int st=1;st<=2*i-1;st++){
			cout<<no<<"\t";
			if(st<i)
                no++;
			else
                no--;
		}
	nso=nso-1;
	no = no+2;
	cout<<endl;
	}
	return 0;
}

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.