# Forming an inverted triangle with asterisks

Page 1 of 1

## 10 Replies - 29534 Views - Last Post: 25 June 2008 - 08:18 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=55732&amp;s=b781cf6bc4439542cc7c51a06438402d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jencalma

Reputation: 0
• Posts: 89
• Joined: 13-July 07

# Forming an inverted triangle with asterisks

Posted 24 June 2008 - 03:55 AM

the output should be::

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

n is where you input the number
for (a=2;a<=a+1;a++,n--)
{ for (b=n;b>0;b--?)
{gotoxy (b,a); cout<<"*";
}
}
getch();
}

* i have a little problem i don't know how to fix this and i really have a problem regarding using the nested for loop.. i know that you can help thank you
* the asterisk are not aligned by one another they descending forming a triangle upside down

This post has been edited by jayman9: 24 June 2008 - 08:28 AM

Is This A Good Question/Topic? 0

## Replies To: Forming an inverted triangle with asterisks

### #2 AndyH1963

Reputation: 22
• Posts: 201
• Joined: 22-June 08

## Re: Forming an inverted triangle with asterisks

Posted 24 June 2008 - 04:21 AM

```#include "stdafx.h"
#include <string>
#include <iostream>

using namespace std;

int main()
{
for (int n = 10; n >= 0; n-=2)
{
if ( n == 0 ) n = 1;
cout << string(n, '*') << endl;
}
}

```

### #3 jencalma

Reputation: 0
• Posts: 89
• Joined: 13-July 07

## Re: Forming an inverted triangle with asterisks

Posted 24 June 2008 - 04:48 AM

AndyH1963, on 24 Jun, 2008 - 04:21 AM, said:

```#include "stdafx.h"
#include <string>
#include <iostream>

using namespace std;

int main()
{
for (int n = 10; n >= 0; n-=2)
{
if ( n == 0 ) n = 1;
cout << string(n, '*') << endl;
}
}

```

where not yet using some string command,, how will i code this program without using some string commands? and where only on the basic of c++ that's why! thank you i appreciate a lot of your help

This post has been edited by jencalma: 24 June 2008 - 04:51 AM

### #4 mensahero

• I Desire...

Reputation: 17
• Posts: 680
• Joined: 26-May 08

## Re: Forming an inverted triangle with asterisks

Posted 24 June 2008 - 04:55 AM

what triangle do you want? upside down or the normal triangle?

I've found this. Maybe you could try it.

```#include<conio.h>

int main()
{

int col, row, inputs, count, a, b, c;

clrscr();

retry:
gotoxy(36,1);
printf("TRIANGLE");
gotoxy(30,3);

printf("Enter number of Rows: ");
scanf("%d",&inputs);

if(inputs > 13){
gotoxy(10,5);
printf("Please enter A number less than 13. Press Enter to Continue..");
getch();
clrscr();
goto retry;
}

row = 5 + inputs;
count = 1;
col = 42-(row*2);

c = inputs*2;
a = 1;

do{

for(a; a <= inputs; a++){

gotoxy(col,row);

for(b = 1; b < c; b++){
printf("  *");
delay (10000);
}

col = col + 3;
c = c - 2;
row--;
}

count++;
}while(count <= inputs);

gotoxy(21, inputs + 10);
printf("Thank you. Press Any Key To Continue....");
getch();

}

```

If that work then cool g00gle is my bestfriend. Thats a normal triangle by the way not the upside down one. The delay is just to show how the triangle is being made, you can thrash it if you want.

This post has been edited by mensahero: 24 June 2008 - 04:57 AM

### #5 AndyH1963

Reputation: 22
• Posts: 201
• Joined: 22-June 08

## Re: Forming an inverted triangle with asterisks

Posted 24 June 2008 - 05:03 AM

[/quote]

where not yet using some string command,, how will i code this program without using some string commands? and where only on the basic of c++ that's why! thank you i appreciate a lot of your help
[/quote]

OK, if you are not using string yet:

```#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
for (int n = 10; n >= 0; n-=2)
{
if (n == 0) n = 1;

for (int m = 0; m < n; ++m)
cout << '*';

cout << endl;
}
}

```

### #6 jencalma

Reputation: 0
• Posts: 89
• Joined: 13-July 07

## Re: Forming an inverted triangle with asterisks

Posted 24 June 2008 - 07:44 AM

AndyH1963, on 24 Jun, 2008 - 05:03 AM, said:

where not yet using some string command,, how will i code this program without using some string commands? and where only on the basic of c++ that's why! thank you i appreciate a lot of your help
[/quote]

OK, if you are not using string yet:

```#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
for (int n = 10; n >= 0; n-=2)
{
if (n == 0) n = 1;

for (int m = 0; m < n; ++m)
cout << '*';

cout << endl;
}
}

```

[/quote]

what i mean is that the triangle should not be aligned on the right side,, it has to descending..is that possible that the triangle are not aligned on neither of the side? Use nested for loops (a total of four (4) for loops). but thank you for your help,, again sorry for the disturbance.. thank you so mch..

This post has been edited by jencalma: 24 June 2008 - 07:47 AM

### #7 AndyH1963

Reputation: 22
• Posts: 201
• Joined: 22-June 08

## Re: Forming an inverted triangle with asterisks

Posted 24 June 2008 - 08:09 AM

jencalma, on 24 Jun, 2008 - 07:44 AM, said:

what i mean is that the triangle should not be aligned on the right side,, it has to descending..is that possible that the triangle are not aligned on neither of the side? Use nested for loops (a total of four (4) for loops). but thank you for your help,, again sorry for the disturbance.. thank you so mch..

If you want to center the triangle:

```#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
const int START = 12;

for (int n = START; n > 0; n-=2)
{
/* output leading space characters, to
center the triangle						  */
for (int x = 0; x < (START - n/2); ++x)
cout << ' ';
/* output the asterisks for this line	   */
for (int m = 0; m < n; ++m)
cout << '*';

cout << endl;
}
}

```

### #8 Jayman

• Student of Life

Reputation: 423
• Posts: 9,532
• Joined: 26-December 05

## Re: Forming an inverted triangle with asterisks

Posted 24 June 2008 - 08:28 AM

Topic renamed to be more descriptive of the problem.

### #9 jencalma

Reputation: 0
• Posts: 89
• Joined: 13-July 07

## Re: Forming an inverted triangle with asterisks

Posted 24 June 2008 - 02:49 PM

AndyH1963, on 24 Jun, 2008 - 08:09 AM, said:

jencalma, on 24 Jun, 2008 - 07:44 AM, said:

what i mean is that the triangle should not be aligned on the right side,, it has to descending..is that possible that the triangle are not aligned on neither of the side? Use nested for loops (a total of four (4) for loops). but thank you for your help,, again sorry for the disturbance.. thank you so mch..

If you want to center the triangle:

```#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
const int START = 12;

for (int n = START; n > 0; n-=2)
{
/* output leading space characters, to
center the triangle						  */
for (int x = 0; x < (START - n/2); ++x)
cout << ' ';
/* output the asterisks for this line	   */
for (int m = 0; m < n; ++m)
cout << '*';

cout << endl;
}
}

```

the program needs you to press enter before the asterisk will show. what i want is that when i run the program the star will appear already on the screen. thank you for the help.

### #10 AndyH1963

Reputation: 22
• Posts: 201
• Joined: 22-June 08

## Re: Forming an inverted triangle with asterisks

Posted 25 June 2008 - 02:41 AM

jencalma, on 24 Jun, 2008 - 09:49 PM, said:

AndyH1963, on 24 Jun, 2008 - 08:09 AM, said:

jencalma, on 24 Jun, 2008 - 07:44 AM, said:

what i mean is that the triangle should not be aligned on the right side,, it has to descending..is that possible that the triangle are not aligned on neither of the side? Use nested for loops (a total of four (4) for loops). but thank you for your help,, again sorry for the disturbance.. thank you so mch..

If you want to center the triangle:

```#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
const int START = 12;

for (int n = START; n > 0; n-=2)
{
/* output leading space characters, to
center the triangle						  */
for (int x = 0; x < (START - n/2); ++x)
cout << ' ';
/* output the asterisks for this line	   */
for (int m = 0; m < n; ++m)
cout << '*';

cout << endl;
}
}

```

the program needs you to press enter before the asterisk will show. what i want is that when i run the program the star will appear already on the screen. thank you for the help.

I don't understand your question? If you need the program to do somthing, then you need to write the code ... Have a go and if you have a problem post the description of the problem and the code to go with it.

### #11 em0napz

Reputation: 0
• Posts: 1
• Joined: 25-June 08

## Re: Forming an inverted triangle with asterisks

Posted 25 June 2008 - 08:18 PM

Try this code:

```#include <iostream.h>
#include <conio.h>

void  main(void)
{
int n;

clrscr();
do {
do {
cout<<"Enter an integer--> ";
cin>>n;
}while(n<=1);

for (int j=1;j<=n;j++)
{
for (int i=n;i>=j;i--)
{
cout<<'*';
}
cout<<endl;
}
cout<<"\nProcess Another? Y/N ";