setw

Write the program that prints following on the screen using setw manipulator.
* * * * *
* * * *
* * *
* *
*
Try this one, but it is not code practice you must use use a couple of for loop
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
#include<conio>
#include<iomanip>
int main()
{
  cout<<setw(5)<<"*****"<<endl;
    cout<<setw(4)<<"****"<<endl;
     cout<<setw(3)<<"***"<<endl;
       cout<<setw(2)<<"**"<<endl;
         cout<<setw(1)<<"*"<<endl;

return 0;
}
with for loop u must try this code.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostrea
#include<conio>
 using nammespace std;
       int main()
       {
	   
	    for(int i=1;i<=5;i++)
	       {

		    for(int j=6-i;j>=1;j--)
		      { cout<<"*"; }
		   cout<<endl;
		 }

		return 0;
	  }





Last edited on
Fatimah jinnah university assignment.. first semester. I already made all assignment for my kazan there. u Want? contact me.
What about using cout.fill() or setfill() in conjunction with setw() ?

That way you have one line initialise, two lines: for loop, and one line tidy up.
Total four lines (plus usual overheads for #include, main() etc.).
Last edited on
Topic archived. No new replies allowed.