Question: Program to print the following pattern?
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
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 = 1; col < row; col++)
- Console.Write("{0} ", col);
- for(col= row; col>=1; col--)
- Console.Write("{0} ", col);
- Console.WriteLine();
- }
- for(row = 1; row<=n; row++)
- {
- for(col = 1; col<=row; col++)
- Console.Write(" ");
- for(col = 1; col<=n-row; col++)
- Console.Write("{0} ", col);
- for(col = n-row-1; col>=1; col--)
- Console.Write("{0} ", col);
- Console.WriteLine();
- }
- Console.ReadLine();
- }
- }
Fig: Screenshot of Output |