Basic Programming C# Examples

C# Program to Print the Floyd’s Triangle

The Program in  C# Program to Print the Floyd’s Triangle is given below:

using System;

namespace FloydsTriangle
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Codeauri Family, enter the number of rows for the Floyd's triangle:");
            int n = int.Parse(Console.ReadLine());

            int number = 1;
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    Console.Write(number + " ");
                    number++;
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
}

Output:

Hello Codeauri Family, enter the number of rows for the Floyd’s triangle:
9
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

Pro-Tips💡

Here are the step by step execution of above program:

  1. The program starts by importing the System namespace, which provides access to various classes and methods, including the Console class.
  2. The program then declares a namespace FloydsTriangle and a class Program within it.
  3. The Main method is the entry point of the program and it contains the logic to print Floyd’s Triangle.
  4. The program starts by displaying a message asking the user to enter the number of rows for the Floyd’s triangle.
  5. The user input is read and parsed into an int variable n.
  6. The number variable is declared and initialized to 1. This variable will store the number that needs to be printed in each iteration.
  7. The program then enters a for loop that will run n times. This loop represents the rows of the triangle.
  8. Within the outer loop, there’s another for loop that will run i times. This loop represents the columns of the triangle.
  9. In each iteration of the inner loop, the value of number is printed using Console.Write and then incremented.
  10. After each inner loop, a new line is printed using Console.WriteLine to start a new row in the triangle.
  11. Finally, the program waits for the user to press the Enter key before ending.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X