Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C++ Number Pattern 38

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

Photo Credit to CodeToFun

C++ Number Pattern 38

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 i, j;
  int k = 1;
  for (i = 1; i <= 5; i++) {
    for (j = 5; j >= i; j--)
      cout << setw(3) << k++;
    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 11 12
13 14 
15

🧠 How the Program Works

Let's break down the logic behind the code:

  1. The program includes two C++ standard libraries: <iostream> for input/output operations and <iomanip> for setting output formatting.
  2. The using namespace std; statement allows you to use elements from the std namespace without explicitly specifying it each time.
  3. The main() function is the entry point of the program. It returns an integer value to indicate the status of program execution (0 for success and non-zero for failure).
  4. The variables i and j are declared as integers to be used as loop counters later in the program.
  5. The variable k is initialized to 1. This variable will be used to print consecutive numbers in the output.
  6. The program enters a nested for loop structure. The outer loop runs from i = 1 to i = 5.
  7. Inside the outer loop, the inner loop runs from j = 5 to j = i. The inner loop's condition j >= i ensures that the inner loop prints the required number of values for each row, decreasing from right to left.
  8. The cout statement inside the inner loop prints the current value of k with a field width of 3 characters using setw(3). The setw() function from <iomanip> is used to set the width of the output, ensuring that each number occupies three characters in the output.
  9. After printing the number, k is incremented by 1 using the post-increment operator k++.
  10. Once the inner loop completes its execution, a newline character "\n" is printed to move the cursor to the next line.
  11. The outer loop continues its iterations until i reaches 5, printing the desired pattern.

💯 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
9 months ago

If you have any doubts regarding this article (C++ Number Pattern 38), 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