Sitemap

Mastering Pattern Problems in Data Structures and Algorithms

6 min readFeb 17, 2025

Problem no 6.

Solution:

 for(int i = n; i > 0 ; i--){
for(int j = 1; j <= i; j++){
cout<<j<<" ";
}
cout<<endl;
}

Problem no. 7.

Solution:

void nStarTriangle(int n) {
// Write your code here.
for(int i = 0; i < n; i++){
for(int space = 0; space < n-i-1; space++){
cout<<" ";
}
for(int star = 0; star < i*2+1; star++){
cout<<"*";
}
cout<<endl;

}
}

Problem no. 8.

Solution:

void nStarDiamond(int n) {
// Write your code here.
for(int i = 0; i < n*2; i++){
if(i<n){
for(int space = 0; space < n-i-1; space++){
cout<<" ";
}
for(int star = 0; star < i*2+1; star++){
cout<<"*";
}
}
else{
int j = i - n;
for(int space = 0; space < j; space++){
cout<<" ";
}
for(int star = 0; star < 2*n - (j*2+1); star++){
cout<<"*";
}
}
cout<<endl;

}
}

Problem no 10.

Solution:

formula is 2n — i for i>n.

void nStarTriangle(int n) {
// Write your code here.
for(int i = 1; i < n*2; i++){
int stars;
if (i > n) {
stars = 2 * n - i;
}else{
stars = i;
}
for(int j = 1; j<=stars; j++){
cout<<"*";
}
cout<<endl;
}
}

Problem no 11.

Solution:

void nBinaryTriangle(int n) {
// Write your code here.
bool k = true;
for(int i = 1; i<=n; i++){
if(i % 2 == 0){
k = false;
}
else{
k = true;
}

for(int j =1; j <= i; j++){

cout<<k<<" ";
k = !k;


}
cout<<endl;
}

Problem no 12.

Solution:

void numberCrown(int n) {
// Write your code here.
for(int i = 1; i<=n; i++){
int space = n*2 - 2*i;
// number
for(int j = 1; j <= i; j++){
cout<<j<<" ";
}

// space
for(int j = 1; j <= space; j++){
cout<<" ";
}

// number
for(int j = i; j >= 1; j--){
cout<<j<<" ";
}
cout<<endl;
}
}

Problem no 13.

Solution:

void nNumberTriangle(int n) {
// Write your code here.
int k = 1;
for(int i = 0; i < n; i++){
for(int j=0;j<=i;j++){
cout<<k<<" ";
k++;
}
cout<<endl;
}
}

Problem no 14.

Solution:

void nLetterTriangle(int n) {
// Write your code here.
for(int i = 0; i < n; i++){
char ch = 'A';
for(int j=0;j<=i;j++){
cout<<ch<<" ";
ch++;
}
cout<<endl;
}
}

Problem no. 15.

Solution:

void nLetterTriangle(int n) {
// Write your code here.
for(int i = n; i > 0; i--){
char ch = 'A';
for(int j=0;j<i;j++){
cout<<ch<<" ";
ch++;
}
cout<<endl;
}
}

Problem no 16.

Solution:

void alphaRamp(int n) {
// Write your code here.
char ch = 'A';
for(int i = 0; i < n; i++){
for(int j = 0; j <= i ; j++){
cout<<ch<<" ";
}
ch++;
cout<<endl;
}
}

Problem no. 17

Solution:


void alphaHill(int n) {
// Write your code here.

for(int i = 0 ; i < n; i++){
int space = n - i - 1;
// space
for(int j = 0 ; j < space; j++){
cout<<" "<<" ";
}
// alpha
char ch = 'A';
int breakpoint = (2*i+1)/2;
for(int j = 1; j<=(2*i + 1); j++){
cout<<ch<<" ";
if (j <= breakpoint) {
ch++;
}else{
ch--;
}

}
// space
for(int j = 0 ; j < space; j++){
cout<<" "<<" ";
}
cout<<endl;
}
}

Problem no 18.

Solution:

void alphaTriangle(int n) {
// Write your code here.


for(int i = 0 ; i < n; i++){
char ch = 'A' + n-1;
for(int j = 0; j <= i; j++){
cout<<ch<<" ";
ch--;
}
cout<<endl;
}
}

Problem no 19.

Solution:

void symmetry(int n) {
// Write your code here.
int space = 0;
for(int i = 0; i< n*2; i++){
// star
for(int j = 1; j <= n-i; j++){
cout<<"*"<<" ";
}
// space
for(int j = 1; j <= space; j++){
cout<<" "<<" ";
}
//star
for(int j = 1; j <= n-i; j++){
cout<<"*"<<" ";
}
space+=2;
cout<<endl;
}

for(int i = 0; i< n; i++){
// star
for(int j = 0; j <= i; j++){
cout<<"*"<<" ";
}
// space
for(int j = 1; j <= space; j++){
cout<<" "<<" ";
}
//star
for(int j = 0; j <= i; j++){
cout<<"*"<<" ";
}
space-=2;
cout<<endl;
}
}

Problem no 20.

Solution:

void symmetry(int n) {
// Write your code here.
// Write your code here.
int space = n*2 - 2;
for(int i = 0 ; i < n; i++){
// star
for(int j = 0; j <= i; j++){
cout<<"*"<<" ";
}
//space
for(int j = 0; j <= i; j++){
cout<<" "<<" ";
}
//star
for(int j = 0; j <= i; j++){
cout<<"*"<<" ";
}
space-=2;
cout<<endl;
}

for(int i = n-1 ; i > 0; i--){
// star
for(int j = i; j > 0; j--){
cout<<"*"<<" ";
}
//space
for(int j = 0; j <= i; j++){
cout<<" "<<" ";
}
//star
for(int j = i; j > 0; j--){
cout<<"*"<<" ";
}
space+=2;
cout<<endl;
}
}

Problem no 21.

Solution:

void getStarPattern(int n) {
// Write your code here.
for(int i = 0; i<n;i++){
if(i == 0 || i == n-1){
for(int j = 0 ; j < n; j++){
cout<<"*";
}
} else{
for(int j=0; j < n; j++){
if(j==0 || j == n-1){
cout<<"*"<<" ";
}else{
cout<<" ";
}
}
}
cout<<endl;
}
}

Problem no. 22.

Solution:

void getNumberPattern(int n) {
// Write your code here.
for(int i =0; i< n*2-1; i++){
for(int j =0; j< n*2-1; j++){
int top = i;
int bottom = j;
int left = (2*n-2) - i;
int right = (2*n-2) - j;

cout<<n-min(min(top,bottom), min(left,right));
}
cout<<endl;
}
}

Thanks…

--

--

Rahul Kumar
Rahul Kumar

Written by Rahul Kumar

MERN Stack Developer building scalable web apps with MongoDB, Express.js, React.js, Node and TS. Sharing insights and best practices for modern web development.

No responses yet