0

Hi i am new to programming and i currently have this code:

   namespace Patterns
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 4; i++)//'rows'
            {
                for (int h = 1; h <= 9 - (i*2)+1; h++)
                {
                    Console.Write("#");
                }

                Console.WriteLine("\n" );

            }
        }
    }
}

This produces this output:

########
######
####
##

the number of hashes is correct as i am going from 8, 6, 4, 2 but i need to add an extra space every time i go onto a new line. How do i make it so the output is as follows?

########
 ######
  ####
   ## 

Thanks, Umer

Some programmer dude
  • 363,249
  • 31
  • 351
  • 550
Umer25
  • 9
  • 1

4 Answers4

1

From your code you could modify it to do the following in the inner for loop:

for (int j = 0; j < i - 1; j++) {
  Console.Write(" ");
}
for (int h = 1; h <= 9 - (i*2)+1; h++) {
  Console.Write("#");
}
Console.WriteLine("\n" );

As a note you should probably use StringBuilder to do this as I believe it is quite inefficient to constantly call Console.WriteLine.

The code could be modified further:

StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 4; i++) {
  for (int j = 0; j < i - 1; j++) {
    sb.append(" ");
  }
  for (int h = 1; h <= 9 - (i*2)+1; h++) {
    sb.append("#");
  }
  sb.append("\n" );
}
Console.WriteLine(sb.toString());
Jkh2
  • 1,010
  • 1
  • 13
  • 25
0

Introduce variables, start your rows at 0 and repeat the string for each row number.

This can also be applied to the string printing the hashes:

static void Main(string[] args)
{   
    int rows = 4;
    int columns = 9;

    for (int i = 0; i < rows; i++)
    {       
        // Print a string with `i` spaces.
        Console.Write(new String(' ', i));

        int hashes = columns - ((i + 1) * 2) + 1;
        Console.Write(new String('#', hashes));

        Console.WriteLine();
    }
}
Community
  • 1
  • 1
CodeCaster
  • 131,656
  • 19
  • 190
  • 236
0

Basically, just add space in front of your hash characters.

######## Row 1 (i=1), 0 Space
 ######  Row 2 (i=2), 1 Space
  ####   Row 3 (i=3), 2 Spaces
   ##    Row 4 (i=4), 3 Spaces

In this case, you need "i-1" spaces for each rows. (Actually, it's (8 - charater count) / 2) and character count was 9 - (i*2) + 1, so ( 8 - 9 + i * 2 - 1 ) / 2 = (i * 2 - 2) / 2 = i - 1 ) So just make loop to add spaces before print hash chracters.

 namespace Patterns
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 4; i++)//'rows'
            {
                for (int j = 0; j < i -1; j++) {
                    Console.Write(" ");
                }
                for (int h = 1; h <= 9 - (i*2)+1; h++)
                {
                    Console.Write("#");
                }

                Console.WriteLine("\n" );

            }
        }
    }
}
papercut
  • 59
  • 2
0

You could do something like this:

for (int i = 1; i <= 4; i++)//'rows'
        {
            for (int h = 1; h <= 9 - (i*2)+1; h++)
            {
                Console.Write("#");
            }

            Console.WriteLine("\n" );
            for (int y = i; y > 0; y--)
            {
                Console.Write(" ");    
            }

        }
jarz
  • 722
  • 7
  • 16