I came across a forum post where original poster wanted to print numbers in a pyramid format using just one for loop.Something like below.
1
2 3
4 5 6
7 8 9 10
Source Code:
static void Main(string[] args)
{
int i = 1,j = 0,m=0;
for (int k = 1; k <= 10; k++)
{
j = 0;
JUMP:if (j < i)
{
Console.Write(m++);
Console.Write('\t');
j++;
goto JUMP;
}
Console.WriteLine();
i++;
}
Console.Read();
}
Similarly for the below structure
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Source Code:
static void Main(string[] args)
{
int i = 1, j = 0;
for (int k = 1; k <= 10; k++)
{
j = 0;
JUMP: if (j < i)
{
Console.Write(k);
Console.Write('\t');
j++;
goto JUMP;
}
Console.WriteLine();
i++;
}
Console.Read();
}
1
2 3
4 5 6
7 8 9 10
Source Code:
static void Main(string[] args)
{
int i = 1,j = 0,m=0;
{
j = 0;
JUMP:if (j < i)
{
Console.Write(m++);
Console.Write('\t');
j++;
goto JUMP;
}
Console.WriteLine();
i++;
}
Console.Read();
}
Similarly for the below structure
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Source Code:
static void Main(string[] args)
{
int i = 1, j = 0;
for (int k = 1; k <= 10; k++)
{
j = 0;
JUMP: if (j < i)
{
Console.Write(k);
Console.Write('\t');
j++;
goto JUMP;
}
Console.WriteLine();
i++;
}
Console.Read();
}
No comments:
Post a Comment