0

I need help on making an isosceles triangle 2sides with 5 asterisks, and a base with 9 asterisks, and has no asterisks inside the triangle.

I've come up with the codes on a 2sides with 5 asterisks and the base with 9 asterisks, but this time, it has asterisks inside the triangle.

#include<iostream.h>

int main()
{ int a, b, c = 5, d=0;
  


   for(a=1;a<=c;a++)

   { 
	   for(b=1;b<=c*2-1;b++)
    
		   if (b<=c+d&&b>=c-d)
   
			   cout<<"*";
   
		   else 
	
			   cout<<" ";
   
			   cout<<"\n";
  
		       d++;
   
   
   }
	return 0;
  }

And it turns out to be like this:


*
***
*****
*******
*********


What i need is the triangle to look like this:


*
* *
* *
* *
*********

Anyone can help me supply the codes for the isosceles triangle with no asterisks inside the triangle? Help would be appreciated.

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by sfuo
0

The solution is in the if() statement where it prints either a * or space.

You should use the C++ headers if you are using C++ ( you were using iostream.h instead of iostream ).

And you should practice formatting your code so it is easier to read because you have cout << "\n"; tabbed out by cout << " "; which kinda looks confusing.

#include <iostream>
using namespace std;

int main()
{
	int a, b, c = 5, d = 0;
	for( a = 1; a <= c; a++ )
	{
		for( b = 1; b <= c*2-1; b++ )
			if( (b == c+d || b == c-d) || a == c )
				cout<<"*";
			else
				cout<<" ";
		cout<<"\n";
		d++;
	}
	return 0;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.