pyramid triangle

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern

In this article, we will discuss the concept of the C# inverted Full Pyramid star pattern program using a for loop.
In this post, we will learn how to write a program in the C# programming language to print the full pyramid star pattern.
To create a full pyramid in C#, you need to print spaces and stars appropriately so that the stars form the pyramid’s shape.
Here’s a simple program to print a full pyramid pattern based on the number of rows provided.

Program 1

using System; 
public class Pyramid_Pattern { 
    public static void Main(string[] args) { 
        //Ask the user for input
        Console.WriteLine ("Enter a number for rows"); 
        //Reading the input from user
        int rows=int.Parse(Console.ReadLine ());//number of rows 
        //outer for loop for each row, 
        for(int i=rows; i>=1; i--){ 
            //inner for loop for print leading spaces to center align the star
            for(int j=0; j<rows-i; j++){ 
                Console.Write (" ");
                } 
                //inner for loop for print stars (2*i-1 stars in each rows)
                for(int k=0; k<(2*i)-1; k++){ 
                    Console.Write ("*"); 
                    
                } 
                    Console.WriteLine (); 
            //move the next line
        } Console.ReadLine (); } }

When the above code is executed, it produces the following result

C# inverted full pyramid

 

Explanation

User input for rows

Console.WriteLine (“Enter a number for rows”);

int rows=int.Parse(Console.ReadLine ());

The program prompts the user to enter the number of rows for the pyramid pattern using  Console.WriteLine();

Console.ReadLine () reads the input from the user as string , and int.Parse converts it into an integer

The number entered by the user determines how many rows the pyramid will have

 

Outer for loop(Rows loop)

for(int i=rows; i>=1; i–)

This loop starts from the value of rows the user enters and decreases until it reaches 1.
It controls the number of rows in the inverted pyramid. For each iteration, one row of the pattern is printed.

 

Inner loop 1 (printing spaces)

for(int j=0; j<rows-i; j++){ Console.Write (” “); }

 

This loop prints spaces before the stars in each row
The number of spaces increases as i decreases to maintain the Pyramid in centre alignment
therefore, the first row has no spaces, the second row has 1 space, and so on.

 

Inner loop 2(printing stars)

for(int k=0; k<(2*i)-1; k++){ Console.Write (“*”); }

This loop uses your print star for each row.
The number of stars in each row is determined by the formula (2*i)-1, which gives the correct number of stars to form the inverted pyramid shape.
Therefore, if the rows=5, the first row will have 9 stars, the second row will have 7 stars, and so on.

 

Curser move to next line

Console.WriteLine ()

After printing each rows , this piece of code moves the cursor to the next line , allowing the next row t be printed the previous one.

 

Similar post

Java program to print star pyramid pattern 

C program to print star pyramid pattern 

C++ program to print star pyramid pattern 

Python program to print star pyramid pattern 

Floyd’s triangle number pattern using for loop in C

Floyd’s triangle pattern using nested for loop in Java

Floyd’s triangle pattern using nested while loop in Java

Hollow pyramid triangle pattern in C++ language

Rhombus pattern in Java using for loop

Rhombus pattern in C using while loop

Rhombus pattern in C++ using do-while loop

 

C# Full Pyramid star pattern program
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

4 weeks ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

4 weeks ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

4 weeks ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

1 month ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

1 month ago

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

7 months ago