Question: Program to print the following pattern?
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
4 3 2 1 2 3 4
3 2 1 2 3
2 1 2
1
Code Snippet:
- using System;
- public class Pattern
- {
- public static void Main()
- {
- int row, col, n, temp;
- Console.Write("Enter the no. of row: ");
- n = Convert.ToInt32(Console.ReadLine());
- temp = n;
- for(row=1; row<=n; row++)
- {
- for(col = 1; col<temp; col++)
- Console.Write(" ");
- temp--;
- for(col = row; col >=1; col--)
- Console.Write("{0} ",col);
- for(col = 2; col<=row; col++)
- Console.Write("{0} ",col);
- Console.WriteLine();
- }
- for(row = 1; row<=n; row++)
- {
- for(col = 1; col <= row; col++)
- Console.Write(" ");
- for(col = n-row; col >=1; col--)
- Console.Write("{0} ", col);
- for(col = 2; col<=n-row; col++)
- Console.Write("{0} ", col);
- Console.WriteLine();
- }
- Console.ReadLine();
- }
- }
Fig: Screenshot of output |