How to print Diamond Pattern in C# ?

Asked 03-Sep-2018
Updated 07-Apr-2023
Viewed 1245 times

1 Answer


0

Printing diamond pattern in C# is a popular exercise for beginners to learn about nested loops and pattern printing. A diamond pattern is a pattern that starts with a single character and then adds one character to each row until the middle row, and then removes one character in each row until the last row.

The basic algorithm to print the diamond pattern in C# involves using nested loops to control the rows and columns of the pattern. The outer loop controls the rows, and the inner loop controls the columns. Here is the basic algorithm:

  1. Get the input from the user, which is the number of rows for the diamond pattern.
  2. Divide the input by 2 and add 1 to get the middle row number.
  3. Use a for loop to iterate through the rows of the pattern, starting from 1 and ending at the input.
  4. Use another for loop to iterate through the columns of the pattern, starting from 1 and ending at the input.
How to print Diamond Pattern in C
  1. If the current row is less than or equal to the middle row number, then check if the current column is equal to the middle column number minus the row number plus 1 or equal to the middle column number plus the row number minus 1.
  2. If the condition in step 5 is true, print the character that you want to use for the pattern.
  3. If the condition in step 5 is not true, print a space character.
  4. If the current row is greater than the middle row number, then check if the current column is equal to the middle column number minus the difference between the current row number and the middle row number or equal to the middle column number plus the difference between the current row number and the middle row number.
  5. If the condition in step 8 is true, print the character that you want to use for the pattern.
  6. If the condition in step 8 is not true, print a space character.