# Printing a triangle of stars

Page 1 of 1

## 3 Replies - 54567 Views - Last Post: 04 April 2008 - 07:44 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=35253&amp;s=72df84acff00f69a12e562eec7f885a9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 r12345t

Reputation: 0
• Posts: 5
• Joined: 12-October 07

# Printing a triangle of stars

Posted 12 October 2007 - 10:55 PM

jayman9, on 19 Sep, 2007 - 11:13 PM, said:

Post the code that you have completed in your attempt to fulfill your assignment. We will not do your work for you.

I want to make a triangle of stars when the user inputs a number. that number inputted should be the length of the base and the height.
For example if user inputs 4:
*
**
***
****

my program when compiled (user enters 4) looks like:

*
**

1/2 of the rows it should be!! i stared at it for hours and just cant figure it out. help would be greatly appreciated.

my code is:
the restrictions i set on the input must be: positive even value between 0 and 21
__________________________________

#include <iostream>
using namespace std;

int main ()
{

int input =0;
do
{
cout<< "Input a number of rows on your triangle"<<endl;
cout<< "Enter -1 to exit"<<endl;
cin>>input;

}while(input<0 || input>21 || input%2 != 0);

int column=1;
int row = 1;

for (row = 1; row<=input; row++)
{
for (column= 1; column<=input; column++)
{
if(column<=row)
cout<< "*";
else
cout<< " ";
column++;

}
column=1;
row++;
cout<<endl;
}

system ("pause");

return 0;

}

i have a feeling its something small but i cant figure it out. please help as quick as possible :) thanks in advance

1) Always use code blocks
2) Always post in the correct forum!

I'm moving this to the C and C++ Forum

This post has been edited by PsychoCoder: 13 October 2007 - 05:48 AM

Is This A Good Question/Topic? 0

## Replies To: Printing a triangle of stars

### #2 csmanoj

Reputation: 11
• Posts: 150
• Joined: 06-August 07

## Re: Printing a triangle of stars

Posted 13 October 2007 - 07:52 AM

You're already incrementing the row variable in the 'for' statement. Then you are again incrementing it inside the loop. So the row variable is incremented twice every iteration. That's why you get only half the number of lines. Remove one of the iterations. Same is the case with columns.

This post has been edited by csmanoj: 13 October 2007 - 08:02 AM

### #3 GDS

Reputation: 1
• Posts: 1
• Joined: 04-April 08

## Re: Printing a triangle of stars

Posted 04 April 2008 - 07:35 AM

Hi
i did some sample code to print the stars in diffrent ways in jave
may be it will be use full check this out.

Thanks
GDS

/**
*
*/

/**
* @author Sashidharrao.GDS
*
*/
public class Traingles {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int n = 10;
Traingles obj = new Traingles();
obj.printTriangleOfStars(n);
obj.printTriangleOfOddNumberStars(n);
obj.printTriangleOfCenteredOddNumberStars(n);
obj.printTriangleOfCenteredOddNumberStarsAndReverse(n);
}

public void printTriangleOfStars(int n) {

for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++)
System.out.print("*");
System.out.print("\n");
}
}

public void printTriangleOfOddNumberStars(int n) {

for (int i = 1, j = 0; i <= n; i++, j++) {
for (int k = 1; k <= (i + j); k++)
System.out.print("*");
System.out.print("\n");
}
}

public void printTriangleOfCenteredOddNumberStars(int rowsize) {

int spaces = rowsize / 2, currentsize = 1, counter, count;
for (counter = 0; counter != (rowsize + 1) / 2; counter++) {
for (count = spaces; count != 0; count--)
System.out.print(" ");
spaces--;
for (count = currentsize; count != 0; count--)
System.out.print("*");
currentsize += 2;
System.out.print("\n");
}
}

void printTriangleOfCenteredOddNumberStarsAndReverse(int rowsize) {
int spaces = rowsize / 2, currentsize = 1, counter, count;
for (counter = 0; counter != (rowsize + 1) / 2; counter++) {
for (count = spaces; count != 0; count--)
System.out.print(" ");
spaces--;
for (count = currentsize; count != 0; count--)
System.out.print("*");
currentsize += 2;
System.out.print("\n");
}
currentsize = rowsize - 1;
spaces += 2;
for (counter = rowsize / 2; counter != 0; counter--) {
for (count = 0; count != spaces; count++)
System.out.print(" ");
spaces++;
for (count = currentsize; count != 0; count--)
System.out.print("*");
currentsize -= 2;
System.out.print("\n");
}
}

}

*
**
***
****
*****
******
*******
********
*********
**********
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
.....*
....***
...*****
..*******
.*********
.....*
....***
...*****
..*******
.*********
..*********
...*******
....*****
.....***
......*

### #4 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: Printing a triangle of stars

Posted 04 April 2008 - 07:44 AM

This topic is from October....