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