Blogger Widgets

How to print following pattern in c++?

How to print following pattern in c++?

I want to print following pattern in c++ using only ONE FOR LOOP..
*
* *
* * *
* * * *
* * * * *


----*
---* *
--* * *
-* * * *


ANSWER:
====================================================================

For the first loop

for(int i = 0; i < 5; i++)
{
cout << std::string(i + 1, '*') << endl;
}

For the second one.

for(int i = 1; i <= 5; i++)
{
cout << std::string(5 - i, '-') << std::string(i, '*') << endl;
}

  //  Be sure to include iostream and string at the top.


No comments:

Post a Comment