This is my code:

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main() {
    int n1 = 1, n2 = 2;
    cout << setw(5) << n1 << endl;
    cout << setw(6) << n1 << " " << n2 << endl;
    cout << setw(7) << n1 << "   " << n2 << endl;
    cout << setw(8) << n1 << "     " << n2 << endl;
    cout << setw(9) << n1 << "       " << n2 << endl;
    cout << setw(8) << n1 << "     " << n2 << endl;
    cout << setw(7) << n1 << "   " << n2 << endl;
    cout << setw(6) << n1 << " " << n2 << endl;
    cout << setw(5) << n1 << endl;

    return 0;
}

This is my output:

My output

However, my intended output is:

Intended output

what's wrong in my code?

1

There are 1 best solutions below

0
Feniks On BEST ANSWER

You just put the numbers in wrong order, try this:

int main() {
    int n1 = 1, n2 = 2;
    cout << setw(9) << n1 << endl;
    cout << setw(8) << n1 << " " << n2 << endl;
    cout << setw(7) << n1 << "   " << n2 << endl;
    cout << setw(6) << n1 << "     " << n2 << endl;
    cout << setw(5) << n1 << "       " << n2 << endl;
    cout << setw(6) << n1 << "     " << n2 << endl;
    cout << setw(7) << n1 << "   " << n2 << endl;
    cout << setw(8) << n1 << " " << n2 << endl;
    cout << setw(9) << n1 << endl;

    return 0;
}