Write a C++ program that prints on the screen following diamond shape with given series of numbers. 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25.
#include <iostream>
#include <iomanip>
using namespace std;
void diamond(int n) {
int x = 1;
for (int i=1; i<=n; i++) {
for (int j=0; j<n-i; j++) {
cout << " ";
}
for (int j=0; j<i-1; j++) {
cout << setw(2) << x++ << " ";
}
cout << setw(2) << x++ << endl;
}
for (int i=n-1; i>0; i--) {
for (int j=0; j<n-i; j++) {
cout << " ";
}
for (int j=0; j<i-1; j++) {
cout << setw(2) << x++ << " ";
}
cout << setw(2) << x++ << endl;
}
}
int main() {
diamond(5);
return 0;
}
Comments
Leave a comment