1

I have a string which starts with only 1 character "$". I have created a loop which runs 4 times, and each time, i want my string to append with 1 extra "$". So when the program runs, it should result in this:

$
$$
$$$
$$$$

Here is my attempt so far:

        string draw = "";
        int counter = 0;

        while (counter < size) // size is 4
        {
            counter++;
            draw += "$\n";
        }

So at the moment it results in:

$
$
$
$

Once i get this too work i would then also like to decrease by 1 each time after it reached the size. So if the size is 4 it should look like this:

$
$$
$$$
$$$$
$$$
$$
$
1
  • Use 2 loops. One that goes to the next and one inner loop that prints characters. So when the outer loop is at 0, the inner loop goes to 1, outer is 1, inner loop goes to 2 and so on. After 3, you keep printing one less char. Commented Sep 10, 2018 at 5:05

6 Answers 6

2

You can use below code

        int size = 4;
        string draw = "";


        while (size>0) // size is 4
        {
            size--;
            draw += "$";
            Console.WriteLine(draw);
        }

        while (draw.Length > 1)
        {
            size++;
            draw = draw.Substring(0, draw.Length - 1);
            Console.WriteLine(draw);
        }
Sign up to request clarification or add additional context in comments.

Comments

2

Because every time you insert a new line character after $ character. I think you should use another variable to store the result. this code will be work:

int size = 4;
string draw = "";
int counter = 0;
string res = "";

while (counter < size) // size is 4
{
    counter++;
    draw += "$";
    res += draw + "\n";
}
while (counter > 0)
{
    counter--;
    draw = draw.Remove(draw.Length - 1, 1);
    res += draw + "\n";
}

It is always better to use StringBuilder instead of just concatenate a string for better performance:

int size = 4;
int counter = 0;
var sb = new StringBuilder();
while (counter < size) // size is 4
{
    counter++;
    sb.Append("$");
    Console.WriteLine(sb.ToString());
}

And for removing a character from the end of your string you can use Remove method like this:

while (counter > 0) // size is 4
{
    counter--;
    sb.Remove(sb.Length - 1, 1);
    Console.WriteLine(sb.ToString());
}

2 Comments

This result in: $$$$, i am also unable to use any other references than system
For each loop you display the result and add a new line char with console.writeline(). If you want to store the result in a seperate strimg, you can define a new string and instead of Console.Writeline, write res = draw + "\n";
1

You Refer below code,

https://www.csharpstar.com/10-different-number-pattern-programs-in-csharp/

       Console.Write("Enter a number: ");
        int n = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine();

        for(int i = 1; i < n; i++)
        {
            for(int j = 1; j <= i; j++)
                Console.Write("$");
            Console.WriteLine();
        }
        for(int i = n; i >= 0; i--)
        {
            for(int j = 1; j <= i; j++)
                Console.Write("$");
            Console.WriteLine();
        }
        Console.WriteLine();

Comments

0

Use 2 string builders:

StringBuilder stringBuilder = new StringBuilder();
StringBuilder lineBuilder = new StringBuilder();

int counter = 0;
int size = 4;
while (counter < size) // size is 4
{
   counter++;
   lineBuilder.Append("$");
   stringBuilder.Append($"{lineBuilder}\n");               
}

string[] lines = stringBuilder.ToString().Split('\n');
while (counter > 0)
{                
   stringBuilder.Append(lines[counter-1]);
   stringBuilder.Append("\n");
   counter--;
}
Console.WriteLine(stringBuilder.ToString());

2 Comments

I am unable to use stringBuilder. the only reference i am too use is system
@TomTok You can in fact use it. Because StringBuilder is in system library.
0

you can use 2 string to do that, and don't need to using StringBuilder in case you don't want to use it:

        string draw = "";
        string result = "";
        int counter = 0;
        int size = 4;

        while (counter < size)
        {
            counter++;
            draw += "$";
            result += draw + "\n";
        }

        Console.WriteLine("Increase $:");
        Console.WriteLine("");
        Console.WriteLine(result);

        while (counter > 0)
        {
            counter--;
            draw = draw.Remove(0, 1);
            result += draw + "\n";
        }

        Console.WriteLine("Decrease $: ");
        Console.WriteLine("");
        Console.WriteLine(result);

Comments

0
using System;

namespace starpattern

{
    class Program

    {
        static void Main(string[] args)

        {

            int i, j;

            for (i = 1; i <= 5; i++)

            {

                for (j = 1; j <= i; j++)

                {

                    Console.Write("$");

                }

                Console.WriteLine();

            }

            Console.ReadLine();

        }

    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.