Diamond Pattern in C++ using For loop of Stars
ADVERTISEMENTS
Diamond pattern in c++ using for loop of stars. In this article, you will learn how to print the diamond pattern in c++ using for loop of stars.
Source Code
// Diamond Pattern in C++ using For loop of Stars
#include <iostream>
using namespace std;
int main() {
int x, c, k, s = 1;
cout << "-----Enter the number of rows-----\n";
cin >> x;
s = x - 1;
for (k = 1; k <= x; k++) {
for (c = 1; c <= s; c++)
cout << " ";
s--;
for (c = 1; c <= 2 * k - 1; c++)
cout << "*";
cout << "\n";
}
s = 1;
for (k = 1; k <= x - 1; k++) {
for (c = 1; c <= s; c++)
cout << " ";
s++;
for (c = 1 ; c <= 2 * (x - k) - 1; c++)
cout << "*";
cout << "\n";
}
return 0;
}
Output
-----Enter the number of rows-----
7
*
***
*****
*******
*********
***********
*************
***********
*********
*******
*****
***
*
7
*
***
*****
*******
*********
***********
*************
***********
*********
*******
*****
***
*