DEV Community

Discussion on: Daily Challenge #2 - String Diamond

Collapse
 
praneetnadkar profile image
Praneet Nadkar

C# Just two main loops to print upper and lower part. One loop to calculate no of rows

var diagonalLength = int.Parse(Console.ReadLine());
if (diagonalLength %2 == 0 || diagonalLength < 0)
{
   Console.WriteLine("null");
   Console.ReadKey();
   return;
}

var rowCount = 0;

// calculate the no of rows needed
for (int i = 1; i < diagonalLength; i += 2)
{
   rowCount += 1;
}
var number = diagonalLength;
var j = rowCount;
// upper part of the diamond
for (int i = 1; i <= number; i += 2, j--)
{
   Console.Write(new string(' ', j));
   Console.Write(new string('*', i));
   Console.WriteLine(new string(' ', j));
}

j = 1;
number = diagonalLength - 2;
// lower diamond
for (int i = number; i >= 1; i -= 2, j++)
{
  Console.Write(new string(' ', j));
  Console.Write(new string('*', i));
  Console.WriteLine(new string(' ', j));
}