Answer to Question #329488 in C++ for Ashiir

Question #329488

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.

1
Expert's answer
2022-04-19T15:28:10-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS