C#: Display the pattern like a pyramid with numbers increased by 1
C# Sharp For Loop: Exercise-13 with Solution
Write a program in C# Sharp to make such a pattern like a pyramid with numbers increasing by 1.
The pattern is as follows:
1 2 3 4 5 6 7 8 9 10
Sample Solution:-
C# Sharp Code:
using System; // Importing necessary namespace
public class Exercise13 // Declaration of the Exercise13 class
{
public static void Main() // Main method, entry point of the program
{
int i, j, spc, rows, k, t = 1; // Declaration of variables 'i', 'j' for iteration, 'spc' for space count, 'rows' to store the number of rows, 'k' for iteration, 't' to store and increase the number
Console.Write("\n\n"); // Displaying new lines
Console.Write("Display the pattern like a pyramid with numbers increased by 1:\n"); // Displaying the purpose of the program
Console.Write("---------------------------------------------------------------"); // Displaying a separator
Console.Write("\n\n");
Console.Write("Input number of rows : "); // Prompting the user to input the number of rows
rows = Convert.ToInt32(Console.ReadLine()); // Reading the number entered by the user
spc = rows + 4 - 1; // Calculating the initial count of spaces for proper alignment
for (i = 1; i <= rows; i++) // Loop to iterate through each row
{
for (k = spc; k >= 1; k--) // Loop to print spaces before the numbers for the pyramid pattern
{
Console.Write(" "); // Displaying spaces
}
for (j = 1; j <= i; j++) // Loop to print the incremented value of 't' in each row 'i' times
{
Console.Write("{0} ", t++); // Displaying 't' and incrementing 't' by 1 for each iteration
}
Console.Write("\n"); // Moving to the next line to form the pyramid pattern
spc--; // Decreasing the space count for the next row
}
}
}
Sample Output:
Display the pattern like pyramid with numbers increased by 1 -------------------------------------------------------------- input number of rows : 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Flowchart:
C# Sharp Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a program in C# Sharp to make such a pattern like right angle triangle with number increased by 1.
Next: Write a program in C# Sharp to make such a pattern like a pyramid with an asterisk.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/csharp-exercises/for-loop/csharp-for-loop-exercise-13.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics