Patterns in C#

WBOY
Release: 2024-09-03 15:33:39
Original
1116 people have browsed it

Patterns are the repeated decorative design. There is a simple code to write patterns in C#. We can write code to print different types of patterns like star pattern, character pattern and number pattern. Below are the various examples to print patterns of star, character and numeric values. These examples consist of loops or nested loops which is a loop inside for a loop. Patterns are a way of designing in sequence or in a logical manner. We can print triangles, pyramids, diamonds, and other symmetries.

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

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();
}
}
}
Copy after login

Output:

Patterns in C#

Conclusion

So above are some examples of various types of patterns. We can print any type of pattern with some changes in the loops.

The above is the detailed content of Patterns in C#. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template