Please comment your opinion on my articles which is very helpful to make new content

C# Logical Patterns: Pyramid and Number Patterns with For Loops

 Patterns are an exciting and practical way to master loops in programming. Using for loops in C#, you can create various logical patterns that visually represent numbers, stars, or shapes. In this article, we’ll explore some commonly used pyramid and number patterns with explanations and examples.


Why Practice Pattern Programming?

  1. Enhances loop logic skills.
  2. Improves understanding of nested loops.
  3. Provides a foundation for complex problem-solving.
  4. You may practice c# logics with previous articles (javascript)

Let’s dive into C# logical patterns with step-by-step code and explanations.


1. Half-Pyramid Pattern with Stars (*)

Explanation

A half-pyramid consists of rows with increasing numbers of stars. Each row contains as many stars as its row number.

Pattern


* ** *** **** *****

Code Example


using System; class HalfPyramid { static void Main() { int rows = 5; for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { Console.Write("*"); } Console.WriteLine(); // Move to the next line } } }

Explanation

  • Outer loop (i) handles the number of rows.
  • Inner loop (j) prints the stars for the current row.

2. Inverted Half-Pyramid Pattern with Stars (*)

Explanation

This pattern starts with a full row of stars and decreases with each subsequent row.

Pattern


***** **** *** ** *

Code Example


using System; class InvertedHalfPyramid { static void Main() { int rows = 5; for (int i = rows; i >= 1; i--) { for (int j = 1; j <= i; j++) { Console.Write("*"); } Console.WriteLine(); // Move to the next line } } }

3. Full Pyramid Pattern with Stars (*)

Explanation

A full pyramid pattern is symmetric and consists of stars centered in a triangle shape.

Pattern


* *** ***** ******* *********

Code Example


using System; class FullPyramid { static void Main() { int rows = 5; for (int i = 1; i <= rows; i++) { // Print spaces for (int j = 1; j <= rows - i; j++) { Console.Write(" "); } // Print stars for (int k = 1; k <= (2 * i - 1); k++) { Console.Write("*"); } Console.WriteLine(); // Move to the next line } } }

Explanation

  • First inner loop prints spaces for alignment.
  • Second inner loop prints stars to form the pyramid.

4. Number Pyramid

Explanation

A number pyramid replaces stars with numbers, often repeating the row number.

Pattern

1 222 33333 4444444 555555555

Code Example


using System; class NumberPyramid { static void Main() { int rows = 5; for (int i = 1; i <= rows; i++) { // Print spaces for (int j = 1; j <= rows - i; j++) { Console.Write(" "); } // Print numbers for (int k = 1; k <= (2 * i - 1); k++) { Console.Write(i); } Console.WriteLine(); // Move to the next line } } }

5. Diamond Pattern with Stars (*)

Explanation

The diamond pattern consists of a full pyramid followed by an inverted pyramid.

Pattern


* *** ***** ******* ********* ******* ***** *** *

Code Example


using System; class DiamondPattern { static void Main() { int rows = 5; // Upper part of diamond for (int i = 1; i <= rows; i++) { for (int j = 1; j <= rows - i; j++) { Console.Write(" "); } for (int k = 1; k <= (2 * i - 1); k++) { Console.Write("*"); } Console.WriteLine(); } // Lower part of diamond for (int i = rows - 1; i >= 1; i--) { for (int j = 1; j <= rows - i; j++) { Console.Write(" "); } for (int k = 1; k <= (2 * i - 1); k++) { Console.Write("*"); } Console.WriteLine(); } } }

6. Number Triangle

Explanation

A number triangle increases the count of numbers in each row.

Pattern


1 12 123 1234 12345

Code Example


using System; class NumberTriangle { static void Main() { int rows = 5; for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { Console.Write(j); } Console.WriteLine(); } } }

7. Pascal’s Triangle

Explanation

Pascal's Triangle consists of binomial coefficients arranged in a triangular format.

Pattern


1 1 1 1 2 1 1 3 3 1 1 4 6 4 1

Code Example


using System; class PascalsTriangle { static void Main() { int rows = 5; for (int i = 0; i < rows; i++) { int number = 1; // Print spaces for (int j = 1; j <= rows - i; j++) { Console.Write(" "); } // Print numbers for (int k = 0; k <= i; k++) { Console.Write(number + " "); number = number * (i - k) / (k + 1); } Console.WriteLine(); } } }

Conclusion

Practicing patterns like pyramids and triangles strengthens your understanding of nested loops in C#. Start with simple patterns like half pyramids and progress to advanced ones like Pascal's Triangle or Diamonds. These patterns are not just academic exercises—they’re also helpful in solving complex programming problems.

For more coding tutorials, visit AJ Tech Blog to explore articles on C# Basics, Operators, and OOP Concepts.

Thnk you for your feedback

Previous Post Next Post

Contact Form