You have for (j=1; j !=i; j++) cout << "#"; that prints some # before the 5.
You have cout << "5"; that prints the 5.
You have cout << endl; that prints the end of the line.
That is what you do for one line.
You say that you want to print some # after the 5, but before the end of the line?
After cout << "5";
Before cout << endl;
How many # in each line? What is the value of i on those lines? Can you caluclate from the value of i the number of # that that line needs?