How to make pattern of stars (*)
Dec 11, 2020 at 6:08am UTC
How to make pattern of stars (*) from this output
1 2 3 4 5
*****
**
* *
* *
* *
I know it's right triangle but the bottom side is in top side and I don't get the logic though?
Here's the my right triangle code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include<iostream>
using namespace std;
int main()
{
int i,j,n;
cout << "input : " ; cin >> n;
for (i=1; i<=n; i++)
{
for (j=1; j<=i; j++)
{
if (j==1 || j==i || i==n)
{ cout<< "*" ; }
else
{ cout << " " ; }
}
cout <<endl;
}
return 0;
}
Last edited on Dec 11, 2020 at 7:00am UTC
Dec 11, 2020 at 6:23am UTC
> I know it's right triangle but the bottom side is in top side and I don't get the logic though?
Here's a hint - for loops can count backwards.
Dec 11, 2020 at 6:59am UTC
is it like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include<iostream>
using namespace std;
int main()
{
int i,j,n;
cout << "input : " ; cin >> n;
for (i=1; i<n; i++)
{ cout << "*" ; }
for (i=1; i<=n; i++)
{
for (j=1; j<=i; j++)
{
if (j==1 || j==i || j==n)
{ cout<< "*" ; }
else
{ cout << " " ; }
}
cout << endl;
}
return 0;
}
Last edited on Dec 11, 2020 at 6:59am UTC
Dec 11, 2020 at 10:00am UTC
Well that seems to produce the required display. However this can be simplified. Consider:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
size_t n {};
cout << "input : " ;
cin >> n;
cout << setw(n + 1) << setfill('*' ) << '\n' << setfill(' ' );
for (size_t i = 2; i <= n; ++i)
cout << '*' << setw(i) << "*\n" ;
}
input : 5
*****
**
* *
* *
* *
Dec 11, 2020 at 11:16am UTC
Hello siabapet,
If the idea is to produce a right triangle consider this:
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 26 27 28 29 30 31 32 33
#include<iostream>
using namespace std;
int main()
{
int numRows {};
cout << "input : " ; cin >> numRows;
for (int i = 0 ; i < numRows; i++)
{
for (int j = 1; j <= i; j++)
{
if (j == 1 || j == i || j == numRows)
{
cout << "*" ;
}
else
{
cout << " " ;
}
}
cout << endl;
}
for (int i = 0 ; i < numRows; i++) // <--- Moved down.
{
cout << "*" ;
}
return 0; // <--- Not required, but makes a good break point.
}
Give your variables a proper name. It makes reading the code easier.
Be careful starting for loops at 1 and using (<=). This will be a problem in the future as most things you will use in a for loop will need to start at zero.
This produced this:
input : 5
*
**
* *
* *
*****
Andy
Dec 11, 2020 at 12:20pm UTC
Hi siabapet,
this is my first reply. I look into your code, but I think what you code is the exactly one.
and I run it to get the result.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include<iostream>
using namespace std;
int main()
{
int i,j,n;
cout << "input : " ; cin >> n;
for (i=1; i<=n; i++)
{
for (j=1; j<=i; j++)
{
if (j==1 || j==i || i==n)
{ cout<< "*" ; }
else
{ cout << " " ; }
}
cout <<endl;
}
return 0;
}
input : 5
*
**
* *
* *
*****
input : 7
*
**
* *
* *
* *
* *
*******
did I misunderstand? do you want the bottom line to top
Last edited on Dec 11, 2020 at 12:20pm UTC
Dec 11, 2020 at 12:23pm UTC
For a right angled triangle, then consider:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
size_t numRows {};
cout << "input : " ;
cin >> numRows;
for (size_t i = 1; i < numRows; ++i)
cout << '*' << setw(i - 1) << ((i > 1) ? "*" : "" ) << '\n' ;
cout << setw(numRows + 1) << setfill('*' ) << '\n' ;
}
input : 7
*
**
* *
* *
* *
* *
*******
Last edited on Dec 11, 2020 at 12:25pm UTC
Topic archived. No new replies allowed.