locked
C# Help!! How to make an asterisk triangle a specified amount of lines high using for loops. RRS feed

  • Question

  • Hello,

    I am learning C# and am currently learning about for loops.

    I have been tasked to make the following program...

    It should ask the user how many lines high they want it then print out the correct triangle. The tringles are triangles formed of rows of asterisks like follows...

          *

       ****

    ********

    But without the spaces, please help me with this, I don't know where to start!?!?

    Monday, August 27, 2012 11:48 AM

Answers

  • int i = 8; //Convert.ToInt32(TextBox1.Text);
    string str = "";
    string str1 = "";

    for (int j = 0; j < i; j++)
    {
        str1 = "";
        for (int l = 0; l <= i - j; l++)
        {
            str = str + " ";
        }
        for (int m = 0; m <= (j - 1); m++)
        {
            str1 = str1 + "*";
        }
        for (int k = 0; k <= j; k++)
        {
            str = str + "*";
        }
        str = str + str1 + System.Environment.NewLine;
    }
    string strTriangle = str;

    you can accept the number from user with a textbox or so.

    regards

    joon



    • Edited by Joon84 Monday, August 27, 2012 12:33 PM
    • Marked as answer by Mike Dos Zhang Thursday, August 30, 2012 7:42 AM
    Monday, August 27, 2012 12:05 PM
  • Hi tomp33,

    I like servy42's suggestion that we should help you learn to become a true programmer not a copier, so I suggest that you should prepare yourself:

    1/ A better understand of the c# language, there are a lot of tutorial for basic C# such as how to use for loop, if then else statement, how to write result to console, Console.Write vs Console.WriteLine etc.. google them

    2/ Understand your question, analyze your question, understand your output, analyze your output. I believe your drawn triangle is not correct because it doesn't have a pattern. Your triangle should look like the following

    *

    **

    ***

    ****

    Do you see the pattern now? this is a triangle which has a high of 4, and 4 is inputted from user. The system then will draw 4 lines of asterisk, for each line (for loop), the next line (WriteLine) has one more asterisk than the previous 1 (something + 1).

    Since you want to write an application that can draw any high of triangle, you should create a variable call n and let user to input this n (Read or ReadLine). However, where should you put n in your code? it should be in somewhere in your for loop code so the computer know how many lines should be drawn.

    Hints: You can use 2 for loops to accomplish this task. First loop tells computer how many asterisk per line, the second tells computer how many line should be drawn.

    Hope this help.

    Sam.

    • Marked as answer by Mike Dos Zhang Thursday, August 30, 2012 7:42 AM
    Monday, August 27, 2012 4:59 PM
  • 2/ Understand your question, analyze your question, understand your output, analyze your output. I believe your drawn triangle is not correct because it doesn't have a pattern. Your triangle should look like the following

    *

    **

    ***

    ****

    Do you see the pattern now? this is a triangle which has a high of 4, and 4 is inputted from user. The system then will draw 4 lines of asterisk, for each line (for loop), the next line (WriteLine) has one more asterisk than the previous 1 (something + 1).

    While it's possible that he didn't post a good example, it's not entirely impossible either.  Imagine that the user provides both a number of lines, and the width of the base.  Assuming the triangle is an isosceles triangle, that's enough information to create it.  Just take the base, and divide it by the number of lines, and then you have the number of asterisks to add to each line (although you'll need to account for the fact that  this may not be an integer number).
    • Marked as answer by Mike Dos Zhang Thursday, August 30, 2012 7:42 AM
    Monday, August 27, 2012 5:29 PM
  • While it's possible that he didn't post a good example, it's not entirely impossible either.  Imagine that the user provides both a number of lines, and the width of the base.  Assuming the triangle is an isosceles triangle, that's enough information to create it.  Just take the base, and divide it by the number of lines, and then you have the number of asterisks to add to each line (although you'll need to account for the fact that  this may not be an integer number).
    Yes it is right that you can find number of asterisk by dividing the max between width base and line by the min between width base and line which mean if width base is greater than line, we will divide it by line width/line and via.
    • Marked as answer by Mike Dos Zhang Thursday, August 30, 2012 7:42 AM
    Monday, August 27, 2012 5:55 PM

All replies

  • int i = 8; //Convert.ToInt32(TextBox1.Text);
    string str = "";
    string str1 = "";

    for (int j = 0; j < i; j++)
    {
        str1 = "";
        for (int l = 0; l <= i - j; l++)
        {
            str = str + " ";
        }
        for (int m = 0; m <= (j - 1); m++)
        {
            str1 = str1 + "*";
        }
        for (int k = 0; k <= j; k++)
        {
            str = str + "*";
        }
        str = str + str1 + System.Environment.NewLine;
    }
    string strTriangle = str;

    you can accept the number from user with a textbox or so.

    regards

    joon



    • Edited by Joon84 Monday, August 27, 2012 12:33 PM
    • Marked as answer by Mike Dos Zhang Thursday, August 30, 2012 7:42 AM
    Monday, August 27, 2012 12:05 PM
  • @Joon, all of the problems with your actual code aside, this is pretty clearly a homework problem.  It is more harmful than helpful to provide full code solutions (especially without any explanation) to such problems.  If you need to help them along a bit then give them a hint or two; guide them into solving the problem on their own.  If you don't they won't learn and it will be that much harder for them to solve the next problem.
    Monday, August 27, 2012 2:10 PM
  • Hi tomp33,

    I like servy42's suggestion that we should help you learn to become a true programmer not a copier, so I suggest that you should prepare yourself:

    1/ A better understand of the c# language, there are a lot of tutorial for basic C# such as how to use for loop, if then else statement, how to write result to console, Console.Write vs Console.WriteLine etc.. google them

    2/ Understand your question, analyze your question, understand your output, analyze your output. I believe your drawn triangle is not correct because it doesn't have a pattern. Your triangle should look like the following

    *

    **

    ***

    ****

    Do you see the pattern now? this is a triangle which has a high of 4, and 4 is inputted from user. The system then will draw 4 lines of asterisk, for each line (for loop), the next line (WriteLine) has one more asterisk than the previous 1 (something + 1).

    Since you want to write an application that can draw any high of triangle, you should create a variable call n and let user to input this n (Read or ReadLine). However, where should you put n in your code? it should be in somewhere in your for loop code so the computer know how many lines should be drawn.

    Hints: You can use 2 for loops to accomplish this task. First loop tells computer how many asterisk per line, the second tells computer how many line should be drawn.

    Hope this help.

    Sam.

    • Marked as answer by Mike Dos Zhang Thursday, August 30, 2012 7:42 AM
    Monday, August 27, 2012 4:59 PM
  • 2/ Understand your question, analyze your question, understand your output, analyze your output. I believe your drawn triangle is not correct because it doesn't have a pattern. Your triangle should look like the following

    *

    **

    ***

    ****

    Do you see the pattern now? this is a triangle which has a high of 4, and 4 is inputted from user. The system then will draw 4 lines of asterisk, for each line (for loop), the next line (WriteLine) has one more asterisk than the previous 1 (something + 1).

    While it's possible that he didn't post a good example, it's not entirely impossible either.  Imagine that the user provides both a number of lines, and the width of the base.  Assuming the triangle is an isosceles triangle, that's enough information to create it.  Just take the base, and divide it by the number of lines, and then you have the number of asterisks to add to each line (although you'll need to account for the fact that  this may not be an integer number).
    • Marked as answer by Mike Dos Zhang Thursday, August 30, 2012 7:42 AM
    Monday, August 27, 2012 5:29 PM
  • While it's possible that he didn't post a good example, it's not entirely impossible either.  Imagine that the user provides both a number of lines, and the width of the base.  Assuming the triangle is an isosceles triangle, that's enough information to create it.  Just take the base, and divide it by the number of lines, and then you have the number of asterisks to add to each line (although you'll need to account for the fact that  this may not be an integer number).
    Yes it is right that you can find number of asterisk by dividing the max between width base and line by the min between width base and line which mean if width base is greater than line, we will divide it by line width/line and via.
    • Marked as answer by Mike Dos Zhang Thursday, August 30, 2012 7:42 AM
    Monday, August 27, 2012 5:55 PM