Diamond pattern program in C++
Track completion, mastery, and revision.
Printing a Diamond Pattern in C++
Creating patterns with characters is a common programming exercise that helps solidify understanding of loops and conditional statements. In this article, we'll walk through how to construct a C++ program to print a diamond shape using asterisks (*).
A typical diamond pattern looks like this:
*
***
*****
***
*
Understanding the Diamond Pattern Logic
The key to printing a diamond pattern is to recognize that it's composed of two symmetrical halves: an upper triangle and a lower triangle.
- Upper Triangle: This part starts with a single asterisk and increases the number of asterisks in each subsequent row, while decreasing the leading spaces, until it reaches the widest part of the diamond.
- Lower Triangle: This part starts from the widest row (or the row immediately after, depending on how
nis defined) and decreases the number of asterisks in each subsequent row, while increasing the leading spaces, until it ends with a single asterisk.
Let's consider n as the number of rows for the upper half of the diamond (including the widest row).
-
For the Upper Triangle (rows
ifrom 1 ton):- The number of leading spaces needed for row
iisn - i. - The number of asterisks needed for row
iis2 * i - 1.
- The number of leading spaces needed for row
-
For the Lower Triangle (rows
ifrom 1 ton - 1):- The number of leading spaces needed for row
iisi. - The number of asterisks needed for row
iis2 * (n - i) - 1. - Notice that
n-ieffectively counts downwards fromn-1to1, creating the decreasing sequence of stars.
- The number of leading spaces needed for row
By combining these two parts, we can form the complete diamond.
C++ Code for Diamond Pattern
Here's the C++ program that implements this logic using nested for loops. We'll prompt the user to enter the number of rows for the upper half of the diamond, which determines its overall size.
#include <iostream>
int main()
{
int i, j, k, space, n;
// Prompt user for input
std::cout << "Enter number of rows for the upper half: ";
std::cin >> n;
std::cout << "\n"; // Add a newline for better formatting
// Part 1: Printing the upper triangle
// This loop iterates for each row of the upper triangle
for (i = 1; i <= n; i++)
{
// Print leading spaces
// 'space' variable is initialized to n-1 and decrements
for (j = 1; j <= (n - i); j++)
{
std::cout << " ";
}
// Print asterisks
// Number of asterisks increases by 2 in each row (1, 3, 5...)
for (k = 1; k <= (2 * i - 1); k++)
{
std::cout << "*";
}
std::cout << "\n"; // Move to the next line after printing a row
}
// Part 2: Printing the lower triangle
// This loop iterates for each row of the lower triangle
// It starts from the row *after* the widest row, hence i from 1 to n-1
for (i = 1; i <= n - 1; i++)
{
// Print leading spaces
// 'space' variable is initialized to 1 and increments
for (j = 1; j <= i; j++)
{
std::cout << " ";
}
// Print asterisks
// Number of asterisks decreases (e.g., if n=5, then 7, 5, 3, 1)
for (k = 1 ; k <= (2 * (n - i) - 1); k++)
{
std::cout << "*";
}
std::cout << "\n"; // Move to the next line after printing a row
}
return 0;
}
Note: We use std::cout and std::cin explicitly instead of using namespace std; for better practice, though the original code used using namespace std;. Both are acceptable for simple programs.
Example Output
When you run the program and enter 5 for the number of rows, you'll see the following output:
Enter number of rows for the upper half: 5
*
***
*****
*******
*********
*******
*****
***
*
Finished reading?