Star pattern programs in C/C++ – 2

Star pattern programs in C/C++ – 2

Pattern programs are widely asked questions in university exams and interviews. These type of questions test logic and concept of a programmer. Every pattern requires different logic. All type of patterns use nested for loops. Some of the pattern uses more than two for loops. Let’s see some star pattern programs in C/C++.

Also read:
1. Star pattern programs in C – 1
2. Number pattern programs – C/C++
3. Alphabet pattern programs in C/C++.

1. Heart pattern with a message

Output-

Program in C :

/*
      * * * * * *             * * * * * *
    * * * * * * * *         * * * * * * * *
  * * * * * * * * * *     * * * * * * * * * *
  * * * * * * * * * * * * * * * * * * * * * *
  * * * * * * * * * * * * * * * * * * * * * *
    * * * * Happy Valentine's Day!! * * * *
      * * * * * * * * * * * * * * * * * *
        * * * * * * * * * * * * * * * *
          * * * * * * * * * * * * * *
            * * * * * * * * * * * *
              * * * * * * * * * *
                * * * * * * * *
                  * * * * * *
                    * * * *
                      * *
*/
#include <stdio.h>
#include <string.h>
int main() {
  int i, j, n = 12;
  char name[25] = "Happy Valentine's Day!! ";
  printf("\n");
  for (i = n / 2; i < n; i += 2) {
    for (j = 1; j < n - i; j += 2)
      printf("  ");
    for (j = 1; j <= i; j++)
      printf("* ");
    for (j = 1; j <= n - i; j++)
      printf("  ");
    for (j = 1; j <= i; j++)
      printf("* ");
    printf("\n");
  }
  printf("  * * * * * * * * * * * * * * * * * * * * * *\n");

  for (i = n; i >= 1; i--) {
    for (j = i; j <= n; j++)
      printf("  ");
    if (i == n - 1) {
      for (j = 1; j < (n / 2) - 1; j++)
        printf("* ");

      printf("%s", name);

      for (j = 1; j < (n / 2) - 1; j++)
        printf("* ");
    }
    else {
      for (j = 1; j < (i * 2) - 1; j++)
        printf("* ");
    }
    printf("\n");
  }
  return 0;
}

Program in C++ :

/*
      * * * * * *             * * * * * *
    * * * * * * * *         * * * * * * * *
  * * * * * * * * * *     * * * * * * * * * *
  * * * * * * * * * * * * * * * * * * * * * *
  * * * * * * * * * * * * * * * * * * * * * *
    * * * * Happy Valentine's Day!! * * * *
      * * * * * * * * * * * * * * * * * *
        * * * * * * * * * * * * * * * *
          * * * * * * * * * * * * * *
            * * * * * * * * * * * *
              * * * * * * * * * *
                * * * * * * * *
                  * * * * * *
                    * * * *
                      * *
*/
#include <iostream>
#include <string.h>
using namespace std;
int main() {
  int i, j, n = 12;
  char name[25] = "Happy Valentine's Day!! ";
  cout << "\n";
  for (i = n / 2; i < n; i += 2) {
    for (j = 1; j < n - i; j += 2)
      cout << "  ";
    for (j = 1; j <= i; j++)
      cout << "* ";
    for (j = 1; j <= n - i; j++)
      cout << "  ";
    for (j = 1; j <= i; j++)
      cout << "* ";
    cout << "\n";
  }
  cout << "  * * * * * * * * * * * * * * * * * * * * * *\n";

  for (i = n; i >= 1; i--) {
    for (j = i; j <= n; j++)
      cout << "  ";
    if (i == n - 1) {
      for (j = 1; j < (n / 2) - 1; j++)
        cout << "* ";

      cout << name;

      for (j = 1; j < (n / 2) - 1; j++)
        cout << "* ";
    }
    else {
      for (j = 1; j < (i * 2) - 1; j++)
        cout << "* ";
    }
    cout << "\n";
  }
  return 0;
}

2. Diamond pattern

Output-

pattern programs in C/C++

Program in C :

/*
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
*/
#include <stdio.h>
int main() {
  int i, j, k, n, r;
  printf("Enter number of rows: ");
  scanf("%d", & r);
  n = r - 1;
  printf("\n");
  for (i = 1; i <= r; i++) {
    for (j = 1; j <= n; j++)
      printf(" ");
    n--;
    for (k = 1; k <= 2 * i - 1; k++)
      printf("*");
    printf("\n");
  }
  n = 1;
  for (i = 1; i <= r - 1; i++) {
    for (j = 1; j <= n; j++)
      printf(" ");
    n++;
    for (k = 1; k <= 2 * (r - i) - 1; k++)
      printf("*");
    printf("\n");
  }
  return 0;
}

Program in C++ :

/*
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
*/
#include <iostream>
using namespace std;
int main() {
  int i, j, k, n, r;
  cout << "Enter number of rows: ";
  cin >> r;
  n = r - 1;
  cout << "\n";
  for (i = 1; i <= r; i++) {
    for (j = 1; j <= n; j++)
      cout << " ";
    n--;
    for (k = 1; k <= 2 * i - 1; k++)
      cout << "*";
    cout << "\n";
  }
  n = 1;
  for (i = 1; i <= r - 1; i++) {
    for (j = 1; j <= n; j++)
      cout << " ";
    n++;
    for (k = 1; k <= 2 * (r - i) - 1; k++)
      cout << "*";
    cout << "\n";
  }
  return 0;
}

3. Hollow triangle pattern

Output-

Program in C :

/*
     *
    * *
   *   *
  *     *
 *       *
** * * * **
*/
#include<stdio.h>
int main() {
  int r, i, j, n;
  printf("Enter number of rows: ");
  scanf("%d", & r);
  for (i = 1; i <= r; i++) {
    for (n = i; n < r; n++)
      printf(" ");
    for (j = 1; j <= (2 * r - 1); j++) {
      if (j == 1 || j == 2 * i - 1 || i == r && j % 2 == 0)
        printf("*");
      else
        printf(" ");
    }
    printf("\n");
  }
  return 0;
}

Program in C++ :

/*
     *
    * *
   *   *
  *     *
 *       *
** * * * **
*/
#include<iostream>
using namespace std;
int main() {
  int r, i, j, n;
  cout << "Enter number of rows: ";
  cin >> r;
  for (i = 1; i <= r; i++) {
    for (n = i; n < r; n++)
      cout << " ";
    for (j = 1; j <= (2 * r - 1); j++) {
      if (j == 1 || j == 2 * i - 1 || i == r && j % 2 == 0)
        cout << "*";
      else
        cout << " ";
    }
    cout << "\n";
  }
  return 0;
}

4. Hollow square pattern

Output-

Program in C :

/*
 *  *  *  *
 *        *
 *        *
 *  *  *  *
*/
#include<stdio.h>
int main() {
  int i, j, n;
  printf("\n Enter side of square: ");
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      if (i == 1 || i == n || j == 1 || j == n)
        printf(" *");
      else
        printf("  ");
    }
    printf("\n");
  }
  return 0;
}

Program in C++ :

/*
 *  *  *  *
 *        *
 *        *
 *  *  *  *
*/
#include<iostream>
using namespace std;
int main() {
  int i, j, n;
  cout << "\n Enter side of square: ";
  cin >>  n;
for (i = 1; i <= n ; i++) {
  for (j = 1; j <= n; j++) {
    if (i == 1 || i == n || j == 1 || j == n)
      cout << " *";
    else
      cout << "  ";
  }
  cout << "\n";
}
return 0;
}

5. Hollow rectangle pattern

Output-

Program in C :

/*
 * * * * * *
 *         *
 * * * * * *
*/
#include<stdio.h>
int main() {
  int i, j, x, y;
  printf("\n Enter length: ");
  scanf("%d", &x);
  printf("\n Enter breadth: ");
  scanf("%d", &y);
  for (i = 1; i <= y; i++) {
    for (j = 1; j <= x; j++) {
      if (i == 1 || i == y || j == 1 || j == x)
        printf(" *");
      else
        printf("  ");
    }
    printf("\n");
  }
  return 0;
}

Program in C++ :

/*
 * * * * * *
 *         *
 * * * * * *
*/
#include<iostream>
using namespace std;
int main() {
  int i, j, x, y;
  cout << "\n Enter length: ";
  cin >>  x;
  cout << " Enter breadth: ";
  cin >>  y;
for (i = 1; i <= y ; i++) {
  for (j = 1; j <= x; j++) {
    if (i == 1 || i == y || j == 1 || j == x)
      cout << " *";
    else
      cout << "  ";
  }
  cout << "\n";
}
return 0;
}

6. Hollow diamond pattern

Output-

Program in C :

/*
    *
   * *
  *   *
 *     *
*       *
 *     *
  *   *
   * *
    *
*/
#include<stdio.h>
int main() {
  printf("Enter the number of rows: ");
  int r, i, j, n = 1, k;
  scanf("%d", & r);
  for (i = 0; i <= r; i++) {
    for (j = r; j > i; j--) {
      printf(" ");
    }
    printf("*");
    if (i > 0) {
      for (k = 1; k <= n; k++) {
        printf(" ");
      }
      n += 2;
      printf("*");
    }
    printf("\n");
  }
  n -= 4;
  for (i = 0; i <= r - 1; i++) {
    for (j = 0; j <= i; j++) {
      printf(" ");
    }
    printf("*");
    for (k = 1; k <= n; k++) {
      printf(" ");
    }
    n -= 2;
    if (i != r - 1) {
      printf("*");
    }
    printf("\n");
  }
  return 0;
}

Program in C++ :

/*
    *
   * *
  *   *
 *     *
*       *
 *     *
  *   *
   * *
    *
*/
#include<iostream>
using namespace std;
int main() {
  cout << "Enter the number of rows: ";
  int r, i, j, n = 1, k;
  cin >> r;
  for (i = 0; i <= r; i++) {
    for (j = r; j > i; j--) {
      cout << " ";
    }
    cout << "*";
    if (i > 0) {
      for (k = 1; k <= n; k++) {
        cout << " ";
      }
      n += 2;
      cout << "*";
    }
    cout << "\n";
  }
  n -= 4;
  for (i = 0; i <= r - 1; i++) {
    for (j = 0; j <= i; j++) {
      cout << " ";
    }
    cout << "*";
    for (k = 1; k <= n; k++) {
      cout << " ";
    }
    n -= 2;
    if (i != r - 1) {
      cout << "*";
    }
    cout << "\n";
  }
  return 0;
}

Parts of program-

1. #include is a pre-processor directive. It is used to include header files.
2. stdio/iostream is header file which has certain commands that c/c++ supports. E.g. return, main, etc.
3. .h is an extension for header file.
4. int is data type. It indicates that the program returns a value.
5. main() is main function that indicates the compiler that the user written programs starts from here.
6. printf/cout is the keyword use to print a message.
7. scanf/cin is the keyword use to store values in variables.
8. return statement is use to return a value. If any error occurs the program will return 0.

Conclusion-

So that’s how we print different star pattern programs in C/C++. We hope it was clear to you. If you find it useful then do share it with your programming buddies and friends. Also if you have any doubt regarding any post do tell us in the comment section.

For daily updates do follow us on Instagram. Visit again!

ErrorFreeProgram.

1 COMMENT

Leave a Reply

Your email address will not be published. Required fields are marked *