Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C++ Number Pattern 62

Posted in C++ Tutorial
Updated on Jan 10, 2024
By Mari Selvan
👁️ 120 - Views
⏳ 4 mins
💬 1 Comment
C++ Number Pattern 62

Photo Credit to CodeToFun

C++ Number Pattern 62

Here`s a program that prints the above number pattern using C++ Programming:

example.cpp
Copied
Copy To Clipboard
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
  int a[10][10], i, j, low = 0, high = 9, n = 1;
  for (i = 0; i < 5; i++, low++, high--) {
    for (j = low; j <= high; j++, n++)
      a[i][j] = n;
    for (j = low + 1; j <= high; j++, n++)
      a[j][high] = n;
    for (j = high - 1; j >= low; j--, n++)
      a[high][j] = n;
    for (j = high - 1; j > low; j--, n++)
      a[j][low] = n;
  }
  cout << "\t\tPerfect Square Spiral\n";
  for (i = 0; i < 10; i++) {
    cout << "\n";
    for (j = 0; j < 10; j++)
      cout << setw(4) << a[i][j];
    cout << "\n";
  }
  return 0;
}

💻 Testing the Program

When you run the above program, it will print the following output:

Output
1    2    3    4    5    6    7    8    9   10
36  37   38   39   40   41   42   43   44   11
35  64   65   66   67   68   69   70   45   12
34  63   84   85   86   87   88   71   46   13
33  62   83   96   97   98   89   72   47   14
32  61   82   95  100   99   90   73   48   15
31  60   81   94   93   92   91   74   49   16
30  59   80   79   78   77   76   75   50   17
29  58   57   56   55   54   53   52   51   18
28  27   26   25   24   23   22   21   20   19

🧠 How the Program Works

Let's break down the logic behind the code:

  1. The program includes the necessary header files, iostream for input/output and iomanip for setting the output format.
  2. The using namespace std; statement allows the program to use names from the std namespace without explicitly specifying it.
  3. The main() function is the entry point of the program.
  4. It declares a 2D array int a[10][10] to store the spiral pattern and initializes some variables i, j, low, high, and n.
  5. The program uses a series of loops to fill the array with numbers in a perfect square spiral pattern. The outer loop runs 5 times (for(i=0; i<5; i++, low++, high--)) because there will be 5 spirals within the 10x10 matrix. The low and high variables are used to define the boundaries of the spiral for each iteration.
  6. Within each iteration of the outer loop, there are four inner loops that fill the matrix with numbers in the spiral pattern:
    1. The first inner loop (for(j=low; j<=high; j++, n++)) fills the top row of the spiral from left to right.
    2. The second inner loop (for(j=low+1; j<=high; j++, n++)) fills the rightmost column of the spiral from top to bottom.
    3. The third inner loop (for(j=high-1; j>=low; j--, n++)) fills the bottom row of the spiral from right to left.
    4. The fourth inner loop (for(j=high-1; j>low; j--, n++)) fills the leftmost column of the spiral from bottom to top.
  7. After filling the array with the perfect square spiral pattern, the program prints the matrix in a formatted way to display the spiral pattern. It first prints a title, "Perfect Square Spiral."
  8. The program then uses nested loops to iterate over the a array and prints its contents using cout and setw(4) (which sets the width of each number printed to 4 characters). This formatting makes the output look like a neat grid.
  9. Finally, the main() function returns 0, indicating successful program execution.

💯 Tips for Enhancement:

Explore the versatility of this pattern by adjusting its parameters. Whether you increase or decrease the size, tweak the spacing, or modify the characters used, each change opens up a world of possibilities, allowing you to customize and create your unique visual effects.

✔ Conclusion:

Creating visually appealing patterns is not only a fun endeavour but also a great way to enhance your programming or design skills. We hope this tutorial has inspired you to explore the world of creative coding. Share your creations with us, and let your imagination run wild!

🤗 Closing Call-to-Action:

We'd love to see your unique interpretations of the number pattern. Share your creations in the comments below, and don't hesitate to reach out if you have any questions or suggestions for future tutorials. Happy coding!

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
11 months ago

If you have any doubts regarding this article (C++ Number Pattern 62) please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy