Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Square Number Patterns

Good day, I am having difficulties in this program
My Code is

using System;
class Pattern{
static void Main()
{
int rows, i, j, k;


Console.WriteLine("Enter the no. of rows: ");
rows = int.Parse(Console.ReadLine());

for (i = 1; i <= rows; i++)
{
 

  for (j = 1; j <= rows; j++)
  {
      k=i+j-1;
      if (k>rows){
      k=k-2;
      }
      Console.Write(k+ " ");
    
    
  }
  Console.WriteLine();
}
}
}

The output that I get is

123
232
323

The output that I want to get is
When I input 3
the output should be

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

123
232
321

When I input 4
the output should be

1234
2343
3432
4321

What seems to be wrong in my code? Thank you

>Solution :

    int count = int.Parse(Console.ReadLine());

    for (int i = 1; i <= count; i++)
    {
        string s = string.Empty;
        for (int j = i; j <= count + i - 1; j++)
        {
            int v;
            if (j > count)
            {
                v = count - (j - count);
            }
            else
            {
                v = j;
            }
            s += v;
        }
        Console.WriteLine(s);
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading