Home > OS >  Square Number Patterns
Square Number Patterns

Time:11-11

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

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

CodePudding user response:

    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);
    }

CodePudding user response:

using System; class Pattern{
    static void Main()
    {
        int rows, i;
        string x = "1";
        
        Console.WriteLine("Enter the no. of rows: ");
        rows = int.Parse(Console.ReadLine());
        
        for(i = 2; i<= rows; i  ){
            x = x  i.ToString();
        }
        for(i = rows-1; i>= 1; i--){
            x = x  i.ToString();
        }
        
        for(i = 0; i< rows; i  ){
            Console.WriteLine(x.Substring(i, rows));
        }
        
    } }

You may try this solution but it's different with using looping for iteration. You may try your own solution, i hope my solution is correct answer.

  •  Tags:  
  • c#
  • Related