Write program in C# to print the pattern shown below.
1.
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
using System;
namespace Pattern1
{
class Program
{
static void Main()
{
int num = 1;
int i, j;
for (i = 1; num <= 55; i++)
{
for (j = 1; j <= i; j++)
{
Console.Write(num.ToString() + " ");
num++;
}
Console.Write("\n");
}
Console.ReadLine();
}
}
}
// Program to print the following output:
// 0 1 0 1 0
// 1 0 1 0 1
// 0 1 0 1 0
// 1 0 1 0 1
// 0 1 0 1 0
// Logic:
// Consider first row as r = 0 and first col as c = 0.
// From output, we observe that whenever r + c is an even no,
// we display a 0 and whenever r + c is odd we display a 1.
using System;
namespace Pattern2
{
class Program
{
static void Main()
{
int[,] x = new int[5, 5];
for (int r = 0; r < 5; r++)
{
for (int c = 0; c < 5; c++)
{
if ((r + c) % 2 == 0)
x[r, c] = 0;
else
x[r, c] = 1;
}
}
for (int r = 0; r < 5; r++)
{
for (int c = 0; c < 5; c++)
{
Console.Write("{0}\t", x[r, c]);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
Write a program to print the following pattern. The number of rows to be printed is input by the user.
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
0 1 0 1 0 1
1 0 1 0 1 0 1
0 1 0 1 0 1 0 1
using System;
namespace Pattern6
{
class Program
{
public static void Main()
{
int h; // height of triangle
Console.WriteLine("Enter no. of rows ");
h = int.Parse(Console.ReadLine());
Console.WriteLine("1");
for (int r = 1; r < h; r++)
{
for(int c = 0; c <= r; c++)
{
if((r + c) % 2 == 0)
Console.Write("1\t");
else
Console.Write("0\t");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
Write a program to print the following pattern. The number of rows to be printed is input by the user.
0 1 0 1 0 1 0 1
1 0 1 0 1 0 1
0 1 0 1 0 1
1 0 1 0 1
0 1 0 1
1 0 1
0 1
1
using System;
namespace Pattern7
{
class Program
{
public static void Main()
{
int h; // height of triangle
Console.WriteLine("Enter no. of rows ");
h = int.Parse(Console.ReadLine());
for (int r = h; r >= 1; r--)
{
for (int c = 1; c <= r; c++)
{
if ((r + c) % 2 == 0)
Console.Write("1\t");
else
Console.Write("0\t");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
Write a C# program to print the following pattern.
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
using System;
namespace Pattern100
{
class Program
{
static void Main()
{
for (int r = 1; r <= 5; r++)
{
Console.WriteLine(" ");
if (r >= 10)
break;
for (int c = 1; c <= 5; c++)
{
// Console.Write(" * ");
Console.Write(r);
if (c == r)
goto loop1;
}
loop1: continue;
}
Console.ReadLine();
}
}
}
Develop a C# code to display the following pattern
1 2 3 4
5 6 7 8
9 10 11 12
using System;
namespace Arrays4
{
class TwoD
{
public static void Main()
{
int r, c;
int[,] x = new int[3, 4];
for (r = 0; r < 3; ++r)
{
for (c = 0; c < 4; ++c)
{
x[r, c] = (r * 4) + c + 1;
Console.Write(x[r, c] + " ");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}