Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views71 pages

Pattern Programs

The document provides various C# programs to print different types of patterns using asterisks, including inverted right angle triangles, diamonds, parallelograms, and hollow shapes. Each pattern is illustrated with code snippets that demonstrate how to generate them in a console application. Additionally, it includes examples of star patterns and character patterns, showcasing the versatility of loops in C# for creating visual designs.

Uploaded by

burrasathwika233
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views71 pages

Pattern Programs

The document provides various C# programs to print different types of patterns using asterisks, including inverted right angle triangles, diamonds, parallelograms, and hollow shapes. Each pattern is illustrated with code snippets that demonstrate how to generate them in a console application. Additionally, it includes examples of star patterns and character patterns, showcasing the versatility of loops in C# for creating visual designs.

Uploaded by

burrasathwika233
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 71

Inverted Right Angle Triangle Pattern

1 class Program

2 {

3 static void Main(string[] args)

4 {

5 for (int row = 8; row >= 1; --row)

6 {

7 for (int col = 1; col <= row; ++col)

8 {

9 Console.Write("*");

10 }

11

12 Console.WriteLine();

13 }
14 Console.ReadLine();

15 }

16 }

Right Angle Triangle Pattern

1 class Program

2 {

3 static void Main(string[] args)

4 {

5 for (int row = 1; row <= 8; ++row)

6 {

7 for (int col = 1; col <= row; ++col)

8 {

9 Console.Write("*");
10 }

11

12 Console.WriteLine();

13 }

14 Console.ReadLine();

15 }

16 }

Diamond Pattern

1 class Program

2 {

3 static void Main(string[] args)

4 {

5 int number = 8, count = 1;


6 count = number - 1;

7 for (var k = 1; k <= number; k++)

8 {

9 for (var i = 1; i <= count; i++)

10 Console.Write(" ");

11 count--;

12 for (var i = 1; i <= 2 * k - 1; i++)

13 Console.Write("*");

14 Console.WriteLine();

15 }

16 count = 1;

17 for (var k = 1; k <= number - 1; k++)

18 {

19 for (var i = 1; i <= count; i++)

20 Console.Write(" ");

21 count++;

22 for (var i = 1; i <= 2 * (number - k) - 1; i++)

23 Console.Write("*");

24 Console.WriteLine();

25 }

26 Console.ReadLine();

27 }

28 }
Right Angle Triangle Reflection Pattern

1 class Program

2 {

3 static void Main(string[] args)

4 {

5 int val = 8;

6 int i, j, k;

7 for (i = 1; i <= val; i++)

8 {

9 for (j = 1; j <= val - i; j++)

10 {

11 Console.Write(" ");

12 }

13 for (k = 1; k <= i; k++)


14 {

15 Console.Write("*");

16 }

17 Console.WriteLine("");

18 }

19 Console.ReadLine();

20 }

21 }

Parallelogram Pattern

1 class Program

2 {

3 static void Main(string[] args)

4 {
5 int size = 8;

6 for (int row = 1; row <= size; ++row)

7 {

8 for (int col = 1; col <= row; ++col)

9 {

10 Console.Write(" ");

11 }

12

13 for (int col = 1; col <= size; ++col)

14 {

15 Console.Write("*");

16 }

17

18 Console.WriteLine();

19 }

20 Console.ReadLine();

21 }

22 }

Hollow Rectangle Pattern


1 class Program

2 {

3 static void Main(string[] args)

4 {

5 int number = 7;

6 for (int i = 0; i < number; i++)

7 {

8 if (i == 0 || i == 6)

9 {

10 for (int j = 0; j < number; j++)

11 {

12 Console.Write("*");

13 }

14 Console.WriteLine();
15 }

16 if (i >= 1 && i <= 5)

17 {

18 for (int j = 0; j < number; j++)

19 {

20 if (j == 0 || j == 6)

21 {

22 Console.Write("*");

23 }

24 else if (j >= 1 && j <= 5)

25 {

26 Console.Write(" ");

27 }

28 }

29 Console.WriteLine();

30 }

31 }

32 Console.ReadLine();

33 }

34 }

Castle Pattern
1 class Program

2 {

3 static void Main(string[] args)

4 {

5 int n = 8;

6 for (int i = 0; i < n; ++i)

7 {

8 Stars(i + 1);

9 Spaces(n - i - 1);

10 Stars(n - i + 1);

11 Spaces(2 * i);

12 Stars(n - i);

13 Spaces(n - i - 1);

14 Stars(i + 1);
15

16 Console.WriteLine();

17 }

18 Console.ReadLine();

19 }

20

21 private static void Stars(int count)

22 {

23 for (int i = 0; i < count; ++i)

24 Console.Write("*");

25 }

26

27 private static void Spaces(int count)

28 {

29 for (int i = 0; i < count; ++i)

30 Console.Write(" ");

31 }

32 }

Pyramid Pattern
1 class Program

2 {

3 static void Main(string[] args)

4 {

5 int x = 8;

6 for (int i = 1; i <= x; i++)

7 {

8 for (int j = 1; j <= (x - i); j++)

9 Console.Write(" ");

10

11 for (int t = 1; t < i * 2; t++)

12 Console.Write("*");

13 Console.WriteLine();

14 }
15 Console.ReadLine();

16 }

17 }

Fair Flag Pattern

1 class Program

2 {

3 static void Main(string[] args)

4 {

5 int space = 0;

6 int max = 10;

8 for (var i = max; i > 0; i--)

9 {
10 for (var j = 0; j < i; j++)

11 {

12 Console.Write("*");

13 }

14 for (var j = 0; j < space; j++)

15 {

16 Console.Write(" ");

17 }

18

19 for (var j = 0; j < i; j++)

20 {

21 Console.Write("*");

22 }

23

24 Console.WriteLine();

25 space += 2;

26 }

27 Console.ReadLine();

28 }

29 }

Hollow Right Angle Triangle Pattern


1 class Program

2 {

3 static void Main(string[] args)

4 {

5 int max = 10;

6 for (var i = 1; i <= max; i++)

7 {

8 for (var j = 1; j <= i; j++)

9 {

10 if (j == 1 || j == i || i == max)

11 {

12 Console.Write("*");

13 }

14 else
15 {

16 Console.Write(" ");

17 }

18 }

19 Console.WriteLine();

20 }

21 Console.ReadLine();

22 }

23 }

Hollow Pyramid Pattern

1 class Program

2 {

3 static void Main(string[] args)


4 {

6 int max = 8;

7 for (var i = 1; i <= max; i++)

8 {

9 for (var j = i; j < max; j++)

10 {

11 Console.Write(" ");

12 }

13 for (var j = 1; j <= (2 * i - 1); j++)

14 {

15 if (i == max || j == 1 || j == (2 * i - 1))

16 {

17 Console.Write("*");

18 }

19 else

20 {

21 Console.Write(" ");

22 }

23 }

24 Console.WriteLine();

25 }

26 Console.ReadLine();
27 }

28 }

Left Arrow Pattern

1 class Program

2 {

3 static void Main(string[] args)

4 {

5 var num = 8;

6 for (var i = -num; i <= num; i++)

7 {

8 var k = i;

9 if (k < 0)

10 {
11 k = k * -1;

12 }

13 for (var j = 0; j <= num; ++j)

14 {

15 if (j < k)

16 {

17 Console.Write(" ");

18 }

19 else

20 {

21 Console.Write("* ");

22 }

23 }

24 Console.WriteLine();

25 }

26 Console.ReadLine();

27 }

28 }

Hollow Diamond Pattern


1 class Program

2 {

3 static void Main(string[] args)

4 {

5 int size = 5;

6 int z = 1;

7 for (int i = 0; i <= size; i++)

8 {

9 for (int j = size; j > i; j--)

10 {

11 Console.Write(" ");

12 }

13

14 Console.Write("*");
15

16 if (i > 0)

17 {

18 for (int k = 1; k <= z; k++)

19 {

20 Console.Write(" ");

21 }

22 z += 2;

23 Console.Write("*");

24 }

25 Console.WriteLine();

26 }

27

28 z -= 4;

29

30 for (int i = 0; i <= size - 1; i++)

31 {

32 for (int j = 0; j <= i; j++)

33 {

34 Console.Write(" ");

35 }

36

37 Console.Write("*");
38

39 for (int k = 1; k <= z; k++)

40 {

41 Console.Write(" ");

42 }

43 z -= 2;

44

45 if (i != size - 1)

46 {

47 Console.Write("*");

48 }

49 Console.WriteLine();

50 }

51 Console.ReadLine();

52 }

53 }

Heart Pattern
1 class Program

2 {

3 static void Main(string[] args)

4 {

5 int size = 5;

6 for (int x = 0; x < size; x++)

7 {

8 for (int y = 0; y <= 4 * size; y++)

9 {

10 double dist1 = Math.Sqrt(Math.Pow(x - size, 2) + Math.Pow(y - size,


2));

11 double dist2 = Math.Sqrt(Math.Pow(x - size, 2) + Math.Pow(y - 3 *


size, 2));

12

13 if (dist1 < size + 0.5 || dist2 < size + 0.5)


14 Console.Write("*");

15 else

16 Console.Write(" ");

17 }

18 Console.WriteLine();

19 }

20

21 for (int x = 1; x < 2 * size; x++)

22 {

23 for (int y = 0; y < x; y++)

24 Console.Write(" ");

25

26 for (int y = 0; y < 4 * size + 1 - 2 * x; y++)

27 {

28 Console.Write("*");

29

30 }

31 Console.WriteLine();

32 }

33 Console.ReadLine();

34 }

35 }
Top 3 types of Patterns in C#
The top 3 types of patterns in c# are mentioned below.

1. Star Pattern
The following are examples to print star patterns.

Example #1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StarPattern
{
class Program
{
static void Main(string[] args)
{
int x, y, z;
for (x =6; x >= 1; x--)
{
for (y = 1; y < x; y++)
{
Console.Write(" ");
}
for (z = 6; z >= x; z--)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

Output:

Example #2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StarPattern
{
class Program
{
static void Main(string[] args)
{
int x, y;
for (x = 1; x <= 6; x++)
{
for (y = 1; y <= x; y++)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

Output:

Example #3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StarPattern
{
class Program
{
static void Main(string[] args)
{
int x, y;
for (x = 5; x >= 1; x--)
{
for (y = 1; y <= x; y++)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

Output:

Example #4

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StarPattern
{
class Program
{
static void Main(string[] args)
{
int x, y, z;
for (x = 5; x >= 1; x--)
{
for (y = 5; y > x; y--)
{
Console.Write(" ");
}
for (z = 1; z <=x; z++)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

Output:

Example #5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StarPattern
{
class Program
{
static void Main(string[] args)
{
int x, y, z;
for (x= 1; x <= 5; x++)
{
for (y = x; y < 5; y++)
{
Console.Write(" ");
}
for (z = 1; z < (x * 2); z++)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

Output:
Example #6

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StarPattern
{
class Program
{
static void Main(string[] args)
{
int x, y, z;
for (x = 5; x >= 1; x--)
{
for (y = 5; y > x; y--)
{
Console.Write(" ");
}
for (z = 1; z < (x * 2); z++)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

Output:
Example #7

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StarPattern
{
class Program
{
static void Main(string[] args)
{
int x, y;
for (x = 1; x <= 5; x++)
{
for (y = x; y < 5; y++)
{
Console.Write(" ");
}
for (y = 1; y <= (2 * x - 1); y++)
{
if (x == 5 || y == 1 || y == (2 * x - 1))
{
Console.Write("*");
}
else
{
Console.Write(" ");
}
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

Output:

Example #8

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CharacterPattern
{
class Program
{
static void Main(string[] args)
{
int x, y;
for (x = 1; x <= 5; x++)
{
for (y = 1; y <= 5; y++)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

Output:

Example #9

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CharacterPattern
{
class Program
{
static void Main(string[] args)
{
int x, y;
for (x = 1; x <= 5; x++)
{
for (y = 1; y <= x; y++)
{
if (y == 1 || y== x || x == 5)
{
Console.Write("*");
}
else
{
Console.Write(" ");
}
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

Output:
2. Number Patterns
The following are examples to print number patterns.

Example #1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NumberPattern
{
class Program
{
static void Main(string[] args)
{
int x, y;
for (x = 1; x <= 5; x++)
{
for (y = 1; y <= x; y++)
{
Console.Write(y);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

Output:
Example #2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NumberPattern
{
class Program
{
static void Main(string[] args)
{
int x, y;
for (x = 5; x >= 1; x--)
{
for (y = 1; y <= x; y++)
{
Console.Write(y);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
Output:

Example #3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NumberPattern
{
class Program
{
static void Main(string[] args)
{
int x, y;
for (x = 5; x >= 1; x--)
{
for (y = x; y <= 5; y++)
{
Console.Write(y);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

Output:

Example #4

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NumberPattern
{
class Program
{
static void Main(string[] args)
{
int x, y;
for (x = 1; x <= 5; x++)
{
for (y = x; y <= 5; y++)
{
Console.Write(y);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

Output:

Example #5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NumberPattern
{
class Program
{
static void Main(string[] args)
{
int x, y;
for (x = 1; x <= 5; x++)
{
for (y = 1; y <= x; y++)
{
Console.Write(x);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

Output:

Example #6

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NumberPattern
{
class Program
{
static void Main(string[] args)
{
int x, y;
for (x = 5; x >= 1; x--)
{
for (y = 5; y >= x; y--)
{
Console.Write(x);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

Output:

Example #7

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NumberPattern
{
class Program
{
static void Main(string[] args)
{
int x, y;
for (x = 5; x >= 1; x--)
{
for (y = 1; y <= x; y++)
{
Console.Write(x);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

Output:

Example #8

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NumberPattern
{
class Program
{
static void Main(string[] args)
{
int x, y;
for (x = 1; x <= 5; x++)
{
for (y = 5; y >= x; y--)
{
Console.Write(x);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

Output:

Example #9

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NumberPattern
{
class Program
{
static void Main(string[] args)
{
int x, y;
for (x = 6; x >= 1; x--)
{
for (y = x; y >= 1; y--)
{
Console.Write(y);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

Output:

Example #10
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NumberPattern
{
class Program
{
static void Main(string[] args)
{
int x, y;
for (x = 1; x <= 5; x++)
{
for (y = 6; y >= x; y--)
{
Console.Write(y);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

Output:
Example #11

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NumberPattern
{
class Program
{
static void Main(string[] args)
{
int x, y;
for (x = 7; x >= 1; x -= 2)
{
for (y = 1; y <= x; y++)
{
Console.Write(y);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

Output:

3. Character Pattern
The following are examples to print character patterns.

Example #1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CharacterPattern
{
class Program
{
static void Main(string[] args)
{
int x, y;
int z = 5;
for (x = 1; x <= z; x++)
{
for (y = 1; y <= x; y++)
{
Console.Write((char)(x + 64));
}
Console.WriteLine("");
}
Console.ReadLine();
}
}
}

Output:

Example #2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CharacterPattern
{
class Program
{
static void Main(string[] args)
{
int x, y;
int z = 5;
for (x = 1; x <= z; x++)
{
for (y = x; y <= z; y++)
{
Console.Write((char)(x + 64));
}
Console.WriteLine("");
}
Console.ReadLine();
}
}
}

Output:

Example #3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CharacterPattern
{
class Program
{
static void Main(string[] args)
{
int x, y;
int z = 5;
for (x = 1; x <= z; x++)
{
for (y = 1; y <= x; y++)
{
Console.Write((char)(z - x + 1 + 64));
}
Console.WriteLine("");
}
Console.ReadLine();
}
}
}

Output:

Example #4

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CharacterPattern
{
class Program
{
static void Main(string[] args)
{
int x, y;
int z = 5;
for (x = 1; x <= z; x++)
{
for (y = x; y<= z; y++)
{
Console.Write((char)(y + 64));
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

Output:

Example #5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CharacterPattern
{
class Program
{
static void Main(string[] args)
{
int x, y, z;
int k = 5;
for (x = 1; x <= k; x++)
{
for (y = 1; y <= k - x; y++)
{
Console.Write(" ");
}
for (z = 1; z <= x; z++)
{
Console.Write((char)(x + 64));
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

Output:
Example #6

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CharacterPattern
{
class Program
{
static void Main(string[] args)
{
int x, y;
int a = 5;
for (x = 1; x <= a; x++)
{
for (y = x; y >= 1; y--)
{
Console.Write((char)(y + 64));
}
Console.WriteLine("");
}
Console.ReadLine();
}
}
}

Output:
Example #7

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CharacterPattern
{
class Program
{
static void Main(string[] args)
{
int x, y;
int a = 5;
for (x = a; x >= 1; x--)
{
for (y = a; y >= x; y--)
{
Console.Write((char)(y + 64));
}
Console.WriteLine("");
}
Console.ReadLine();
}
}
}

Output:

Example #8

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CharacterPattern
{
class Program
{
static void Main(string[] args)
{
int x, y;
int a = 5;
for (x = 1; x <= a; x++)
{
for (y = a; y >= x; y--)
{
Console.Write((char)(y + 64));
}
Console.WriteLine("");
}
Console.ReadLine();
}
}
}

Output:

Example #9

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CharacterPattern
{
class Program
{
static void Main(string[] args)
{
int x, y;
int z = 5;
for (x = z; x >= 1; x--)
{
for (y = x; y >= 1; y--)
{
Console.Write((char)(y + 64));
}
Console.WriteLine("");
}
Console.ReadLine();
}
}
}

Output:

Example #10

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CharacterPattern
{
class Program
{
static void Main(string[] args)
{
int x, y;
int z = 6;
for (x = 1; x <= z; x++)
{
for (y = 1; y<= z - x; y++)
{
Console.Write(" ");
}
for (y = 1; y <= x; y++)
{
Console.Write((char)(y + 64));
}
for (y = x - 1; y >= 1; y--)
{
Console.Write((char)(y + 64));
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

Output:
Pattern 1:

class pyramid
{
public static void Main()
{
int num, space;

while (true)
{
Console.Write("Enter a number between 1 to 9 : ");

num = Convert.ToInt32(Console.ReadLine());

space = num - 1;

for (int i = 1; i <= num; i++)


{
for (space = 1; space <= (num - i); space++)
{
Console.Write(" ");
}

for (int j = 1; j <= i; j++)


{
Console.Write(j);
}

for (int k = (i - 1); k >= 1; k--)


{
Console.Write(k);
}

Console.WriteLine();
}
}

}
}

Try now in dotnetfiddle here.

Pattern:2
1
22
333
4444
55555

class NumberPattern
{
public static void Main()
{
int no = 5;

for (int i = 1; i <= 5; i++)


{
for (int j = 1; j <= i; j++)
{
Console.Write(i);
}
Console.WriteLine();
}
Console.ReadKey();
}
}

Pattern:3
1
12
123
1234
12345
1234
123
12
1
You can try this pattern online here.
class Program
{

public static void Main(string[] args)


{
Console.Write("Enter a number: ");
int n = Convert.ToInt32(Console.ReadLine());

Console.WriteLine();

for(int i = 1; i < n; i++)


{
for(int j = 1; j <= i; j++)
Console.Write(j.ToString());
Console.WriteLine();
}
for(int i = n; i >= 0; i--)
{
for(int j = 1; j <= i; j++)
Console.Write(j.ToString());
Console.WriteLine();
}
Console.WriteLine();
}
}

Pattern:4
12345
1234
123
12
1

1
12
123
1234
12345

class Program
{

public static void Main(string[] args)


{
Console.Write("Enter a number: ");
int n = Convert.ToInt32(Console.ReadLine());

Console.WriteLine();
for (int i = n; i >= 0; i--)
{
for (int j = 1; j <= i; j++)
Console.Write(j.ToString());
Console.WriteLine();
}

for(int i = 1; i <= n; i++)


{
for(int j = 1; j <= i; j++)
Console.Write(j.ToString());
Console.WriteLine();
}

Console.WriteLine();
}
}

5. Pattern:5
1
01
101
0101
10101

class Program
{

public static void Main(string[] args)


{
int i, j, n, p, q;

Console.Write("\n\n");
Console.Write("Print the Floyd's Triangle:\n");
Console.Write("-----------------------------");
Console.Write("\n\n");
Console.Write("Input number of rows : ");
n = Convert.ToInt32(Console.ReadLine());
for (i = 1; i <= n; i++)
{
if (i % 2 == 0)
{ p = 1; q = 0; }
else
{ p = 0; q = 1; }
for (j = 1; j <= i; j++)
if (j % 2 == 0)
Console.Write("{0}", p);
else
Console.Write("{0}", q);
Console.Write("\n");

}
}
}

Pattern: 6
1
23
456
7 8 9 10

public static void Main(string[] args)


{
int i, j, rows, k = 1;

Console.Write("\n\n");
Console.Write("Display the pattern like right angle triangle with
number increased by 1:\n");
Console.Write("----------------------------------------------------
-----------------------");
Console.Write("\n\n");

Console.Write("Input number of rows : ");


rows = Convert.ToInt32(Console.ReadLine());
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
Console.Write("{0} ", k++);
Console.Write("\n");
}
}

Pattern:7
1
1 2
1 2 3
1 2 34
1 2 345
1 2 34
1 2 3
1 2
1

class Program
{

public static void Main(string[] args)


{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write("" + (j));

}
Console.WriteLine("");

for (int k = 4; k >= 0; --k)


{

for (int j = 1; j <= k; j++)


{
Console.Write("" + (k));

}
Console.WriteLine("");
}
}
}

Pattern:8

public static void Main(string[] args)


{
int row, i, j, k;
Console.Write("Enter the no. of row you want to print: ");
row = Convert.ToInt32(Console.ReadLine());

for (i = 0; i <= row; i++)


{
k = 1;
for (j = i; j <= row - 1; j++)
Console.Write(" ");

for (j = 0; j <= i; j++)


{
Console.Write("{0} ", k);
k = (k * (i - j) / (j + 1));
}
Console.WriteLine();
}
Console.ReadLine();
}

Pattern 9:
5
54
543
5432
54321

public static void Main(string[] args)


{
{
int i = 5;
while (i >= 1)
{
int j = 5;
while (j >= i)
{
Console.Write(j);
j--;
}
i--;
Console.WriteLine();
}
Console.Read();
}
}

Pattern:10

public static void Main(string[] args)


{
int i, j, k;

for (i = 1; i <= 7; i++)


{
for (j = 1; j <= i; ++j)
Console.Write(j);

for (k = 7 - i; k >= 1; k--)


Console.Write("*");

Console.Write("\n");
}

Console.ReadLine();
}

-----------------------------------------------------------------------------------------------------------------------------------

Different Star Pattern Programs in C#


CsharpProgrammingServer Side Programming

Like any other programming languages such as C, C++, Java, we can easily display start
pattern programs in C#.

Let us see them one by one.

Example
Live Demo

using System.IO;
using System;
class Program {
static void Main() {
for (int i = 1; i <= 6; ++i) {
for (int j = 1; j <= i; ++j) {
Console.Write("*");
}
Console.WriteLine();
}
}
}

Output
*
**
***
****
*****
******

Let us see another series.

Example

using System.IO;
using System;
class Program {
static void Main() {
int i, j, k;
int n = 6,
// loop to print the series
for (i = 1; i <= n; i++) {
for (j = 1; j <= n - i; j++) {
Console.Write(" ");
}
for (k = 1; k <= i; k++) {
Console.Write("*");
}
Console.WriteLine("");
}
Console.ReadLine();
}
}

Output
*
**
***
****
*****
******

Let us see another series.

Example

using System.IO;
using System;
class Program {
static void Main() {
for (int i = 6; i >= 1; --i) {
for (int j = 1; j >= i; ++j) {
Console.Write("*");
}
Console.WriteLine();
}
}
}

Output
******
*****
****
***
**
*

Here is another series.

Example
Live Demo

using System.IO;
using System;
class Demo {
static void Main() {
int a = 1, spaces, k = 6, i = 0, j = 0;
spaces = k - 1;
for(i=1; i<k*2; i++) {
for(j=1; j<=spaces; j++) {
Console.Write(" ");
}
for(j=1; j<a*2; j++) {
Console.Write("*");
}
Console.WriteLine("");
if(i < k) {
spaces--;
a++;
} else {
spaces++;
a--;
}
}
}
}

Output
*
***
*****
*******
*********
***********
*********
*******
*****
***
*

You might also like