# Most Asked Pattern Programs And Solutions ### Most Asked Pattern Programs And Solutions

|   |  0
Programming

Yesterday I participated in a small coding competition event with one of my friends in first time of my life. Competitions can also be a way to connect with top employers or top talented people. Regardless of our formal educational background, if we have the skills to win a competition, we got a chance to connect with people, that's why we must use every single chance.

But unfortunately, we didn't get the first prize because of some other reasons - we got third. Stubbornness, That must be settle as soon as possible. I'm sharing my knowledge with you and one of the simplest problem they asked, also about similar problems and its solutions. Now, I'm competing with myself only and did the job.

The question is to print a hollow diamond inscribed in a rectangle * pattern. Yes, it is very simple if you think and if you got a little time to plan what you going to do. Of course, planning is the first step before starting to code and involves the analysis of the conditions. Don't do coding without well planning especially for this kind of stuffs. Some of these patterns have particular importance in mathematics and some of them are symmetrical, while others are not.

### Hollow diamond inscribed in a rectangle. ``````#include
int main() {
int i, j, n, dn;
printf("Enter input count :");
scanf("%d", &n);

dn = 2 * n; // double of n

// first half of the * pattern
for(i = 0; i < n; i++) {
for(j = 0; j < dn; j++) {
// printing first left triangle
if(i + j <= n - 1)
printf("*");
else
printf(" ");
// printing first right triangle
if((i + n) <= j)
printf("*");
else
printf(" ");
}
printf("\n");
}

// second half of the * pattern
for(i = 0; i < n; i++) {
for(j = 0; j < dn; j++) {
// printing second left triangle
if(i >= j)
printf("*");
else
printf(" ");
// printing second right triangle
if(i >= (dn - 1) - j)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
``````

### Left and Right Arrows. ``````#include
int main() {
int i, j, n;
// n must be odd number
printf("Enter an odd number: ");
scanf("%d", &n);
int number = n / 2 * 3;
// right arrow
printf("1. Right Arrow: \n");
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
// center horizontal, // top right diagonal, // bottom right diagonal
if(i == n / 2 || j - i == n / 2 || i + j == number)
printf("*");
else
printf(" ");
}
printf("\n");
}
// left arrow
printf("\n\n2. Left Arrow: \n");
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
// center horizontal, // bottom left diagonal, // top left diagonal
if(i == n / 2 || i - j == n / 2 || i + j == n / 2)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
``````

### Rhombus Pattern. ``````#include
int main() {
int i, j, n;
printf("Enter row count: ");
scanf("%d", &n);

// code for solid rhombus
printf("1. Solid Rhombus:\n");
for(i = 0; i < n; i++) {
for(j = 0; j < n - i; j++) {
}
for(j = 0; j < n; j++) {
printf("*");
}
printf("\n");
}

// code for hollow rhombus
printf("\n2. Hollow Rhombus:\n");
for(i = 0; i < n; i++) {
for(j = 0; j < n - i; j++) {
}
for(j = 0; j < n; j++) {
// upper horizontal, bottom horizontal, left diagonal, right diagonal
if(i == 0 || i == n - 1 || j == 0 || j == n - 1)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
``````

### Butterfly Pattern. ``````#include
int main() {
int i, j, n;
printf("Enter a number: ");
scanf("%d", &n);
// top half of the butterfly
for(i = 0; i < n; i++) {
for(j = 0; j < (2 * n); j++) {
if(i >= j)  // top left triangle
printf("*");
else
printf(" ");
if(i >= (2 * n - 1) - j) // top right triangle
printf("*");
else
printf(" ");
}
printf("\n");
}
// bottom half of the butterfly
for(i = 0; i < n; i++) {
for(j = 0; j < (2 * n); j++) {
if(i + j <= n - 1) // bottom left triangle
printf("*");
else
printf(" ");
if((i + n) <= j) // bottom right triangle
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
``````

### Solid Pyramid Pattern. ``````#include
int main() {
int i, j, n, k=0;
printf("Enter a number: ");
scanf("%d", &n);

// Half Pyramid
printf("Half Pyramid: \n");
for(i = 0; i < n; i++) {
for(j = 0; j <= i; j++) {
printf("* ");
}
printf("\n");
}

printf("\nInverted half Pyramid: \n");
for(i = n; i >= 1; --i) {
for(j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}

printf("\nFull Pyramid: \n");
for(i = 1; i <= n; ++i, k = 0) {
for(j = 1; j <= n-i; ++j) {
printf(" ");
}
while(k != 2 * i-1) {
printf("*");
++k;
}
printf("\n");
}

printf("\nInverted Full Pyramid: \n");
for(i=n; i>=1; --i) {
for(j=0; j < n-i; ++j)
printf(" ");

for(j=i; j <= 2*i-1; ++j)
printf("*");

for(j=0; j < i-1; ++j)
printf("*");

printf("\n");
}
return 0;
}
``````

### Hollow Pyramid Pattern. ``````#include
int main() {
int i, j, n, k=0;
printf("Enter a number: ");
scanf("%d", &n);

printf("Hollow full pyramid: \n");
for (i = 1; i <= n; i++) {
for (j = i; j < n; j++) {
printf(" ");
}
while (k != (2 * i - 1)) {
if (k == 0 || k == 2 * i - 2)
printf("*");
else
printf(" ");
k++;
}
k = 0;
printf("\n"); // print next row
}
for (i = 0; i < 2 * n - 1; i++) {
printf("*");
}
printf("\n");

printf("\nInverted Hollow Half pyramid: \n");
for (i = 0; i < n; i++) {
printf("* ");
}
for(i=n; i>=1; --i) {
for(j=0; j < n-i; ++j)
while (k != (2 * i - 1)) {
if (k == 0 || k == 2 * i - 2)
printf("*");
else
printf(" ");
k++;

}
k = 0;
printf("\n"); // print next row
}

printf("\nInverted Hollow Full Pyramid: \n");
for (i = 1; i <= n; i++) {
for (j = 1; j < i; j++) {
printf(" ");
}
for (j = 1; j <= (n * 2 - (2 * i - 1)); j++) {
if (i == 1 || j == 1 || j == (n * 2 - (2 * i - 1))) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}

return 0;
}
`````` 506 Claps