Inverted Pyramid of Numbers Pattern Program in C#
3. Write a program to print the following Pattern
4 3 2 1
4 3 2
4 3
4
Inverted Pyramid of Numbers Pattern Program in C#
3. Write a program to print the following Pattern
4 3 2 1
4 3 2
4 3
4
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sachin SinghPosted Jul 30, 2022, 7:55 AM
Vishal JoshiPosted Jul 29, 2022, 6:31 AM
static void printPattern(int n)
{
// Loop to print desired pattern
int curr_Index = n;
for (int line_no = n; line_no > 0;)
{
// If current index count is Greater than 0
// current line number
if (curr_Index > 0)
{
Console.Write(curr_Index + (n - line_no));
curr_Index--;
continue;
}
// Else time to print a new line
if (curr_Index <= 0)
{
Console.WriteLine();
line_no--;
curr_Index = line_no;
}
}
}
Desired Output with single Loop Only
4 3 2 1
4 3 2
4 3
4
Vishal YelvePosted Jul 29, 2022, 5:57 AM
public static void Main()
{
Console.Write("Enter Number of Rows :");
int rows = Convert.ToInt32(Console.ReadLine());
for (int i = rows; i >= 1; i--)
{
//j<=i //let rows = 5
/* first row (0 to 5) = 5 numbers (1,2,3,4,5)
* second row (0 to 4) = 4 numbers (1,2,3,4)
* third row (0 to 3) = 3 numbers (1,2,3)
* and so on
*/
//calculate stars for each row one by one
for (int j = 1; j <= i; j++)
{
Console.Write(j + " ");
}
Console.WriteLine();
}
Console.ReadLine();
}
Enter Number of Rows: 5
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1