Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Display the pattern in which the first and a last number of each row will be 1

C# Sharp For Loop: Exercise-36 with Solution

Write a program in C# Sharp to display the such a pattern for n number of rows using a number which will start with the number 1 and the first and a last number of each row will be 1.
The pattern is as follows :

  1
 121
12321

Pictorial Presentation:

C# Sharp Exercises: Display the pattern in which the first and a last number of each row will be 1

Sample Solution:

C# Sharp Code:

using System;  
public class Exercise36  
{  
    public static void Main()
{
   int i,j,n;
   
	Console.Write("\n\n");
    Console.Write(" Display the pattern in which first and last number of each row will be 1:\n");
    Console.Write("----------------------------------------------------------------------------");
    Console.Write("\n\n");     
   
   Console.Write("Input number of rows : ");
   n = Convert.ToInt32(Console.ReadLine());    
   for(i=0;i<=n;i++)
   {
     /* print blank spaces */
     for(j=1;j<=n-i;j++)
	Console.Write(" ");
     /* Display number in ascending order upto middle*/
     for(j=1;j<=i;j++)
       Console.Write("{0}",j);
 
     /* Display  number in reverse order after middle */
       for(j=i-1;j>=1;j--)
	  Console.Write("{0}",j);
 
     Console.Write("\n");
   }
  } 
}

Sample Output:

 Display the pattern in which first and last number of each row will be 1:                                  
----------------------------------------------------------------------------                                
Input number of rows : 5                                                                                    
    1                                                                                                       
   121                                                                                                      
  12321                                                                                                     
 1234321                                                                                                    
123454321

Flowchart:

Flowchart: Display the pattern in which first and last number of each row will be 1

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to display the first n terms of Fibonacci series.
Next: Write a program in C# Sharp to display the number in reverse order.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.