Hello, guys I'm hoping that you can help me. I'm having a problem coding w/ this problem. I find it hard to do this bcoz when I run the program the result always be like this.

    #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;
    }
    system ("pause");
    return 0;
    }

result:

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

I don't know what to do. the problem is written below.
If someone could help me figure this kind of triangles (below) it will be great.
I will really aprreciate your effort guys. Ty

=====================================

Write a program that outputs a triangle made out of asterisks. The size of the triangle depends on the input of the user. For example, if the user inputs 3, the triangle will look like that of Triangle 1, if the input is 5, the triangle will look like Triangle 2, and if the input is 7, the triangle will look like Triangle 3. Hint: (1) Use one loop for asterisks and another loop for spaces or blanks (2) There are a total of three loops.

Answer the problem by providing three different program codes which use different loop statements:

  1. for loop
  2. while loop
  3. do…while loop

Triangle 1

  *
 * *
* * *

Triangle 2

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

Triangle 3

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

Recommended Answers

All 3 Replies

It is a golden rule never to change the loop counter. In your case leave out line 9. First print spaces, then decrement the number of spaces to print, then print asterixes,thenincrement number of asterixes to print

The reason yours doesn't look like a triangle is because you need to print spaces before the asterisk. There's also a space between each asterisk. So in Trianble 1 might look like this

bb*
b*b*
*b*b*

Where 'b' is a space

I also think the user input needs to be an odd number in order for the triangle to look right.

As the Hint in your problem mentions use different loops, one to count the rows, then 2 inside that one. One to count the asterisks and one to count the spaces. A further hint build the beginning spaces and the asterisks into separate strings.

If you do it right, it will look right regardless of the number input because of the extra spaces between the asterisks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.