Back to: C#.NET Programs and Algorithms
Half Pyramid of Numbers Pattern Program in C#
In this article, I am going to discuss How to implement the Half Pyramid of Numbers Pattern Program in C# with Examples. Please read our previous article where we discussed the Inverted Pyramid of Numbers Pattern Program in C#. Please have a look at the below image which shows the Half-Inverted Pyramid of Numbers Pattern.
Understanding Half Pyramid of Numbers Pattern in C#
- In the first row, we want 1 and 4 blank spaces.
- In the second row, we want 1 and 2, and 3 blank spaces.
- In the third row, we want 1,2 and 3, and 2 blank spaces.
- In the fourth row, we want 1,2,3, 4, and 1 blank space.
- In the fifth row, we want 1,2,3,4, 5, and 0 blank spaces.
- This list will go on as on the number of rows.
- From the above pattern, it is clear that at each row numbers are increasing +1 at each row.
- For example, the 1st row has 1, the 2nd row has 1 and 2 and the 3rd row has 1,2, and 3, and so on.
- From the above pattern, it is clear that in each row number of blank spaces is decreasing by –1 at each row. For example, the 1st row has 4 spaces, the 2nd row has 3 spaces, the 3rd row has 2 spaces, and so on.
How to implement the Half Pyramid of Numbers Pattern Program in C#?
To solve the above pattern,
- We are going to use two for loops. The outer for loop and the inner for loop.
- The outer for loop will be used to handle the rows one by one and the inner for loop will be used to handle the columns one by one.
- From the above explanation, it is clear that the number in each row is equal to the current row.
- 1st row has 1, 2nd row has 1 and 2, and so on.
- The inner loop will be used to print the numbers according to row and the outer loop’s job is to go on a new line after printing the current row numbers.
Program to Print Half Pyramid of Numbers Pattern in C#
The following C# Program will allow the user to input the number of rows and then print the Half Pyramid of Numbers Pattern on the console.
using System; namespace PatternDemo { public class HalfPyramidOfNumbersPattern { public static void Main() { Console.Write("Enter number of rows :"); int rows = Convert.ToInt32(Console.ReadLine()); for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { Console.Write(j + " "); } Console.WriteLine("\n"); } Console.ReadKey(); } } }
Output:
In the next article, I am going to discuss the Half Pyramid of Numbers Pattern Program in C# with Examples. Here, in this article, I try to explain How to implement the Half Pyramid of Numbers Pattern Program in C# with Examples and I hope you enjoy this Half Pyramid of Numbers Pattern Program in C# article.