w3resource
C# Sharp Programming Exercises

C# Sharp Exercises: Display the pattern like pyramid using the alphabet

C# Sharp For Loop: Exercise-40 with Solution

Write a C# Sharp Program to display the following pattern using the alphabet.
The pattern is as follows:

A
A B A 
A B C B A
A B C D C B A 

Pictorial Presentation:

C# Sharp Exercises: Display the pattern like pyramid using the alphabet

Sample Solution:

C# Sharp Code:

using System;  
public class Exercise40
{  
    public static void Main() 
  {
   int i, j;
   char alph = 'A';
   int n;
   int ctr = 1;
  
	Console.Write("\n\n");
    Console.Write("Display the pattern like pyramid using the alphabet:\n");
    Console.Write("------------------------------------------------------");
    Console.Write("\n\n");     
 
   Console.Write("Input the number of Letters (less than 26) in the Pyramid : \n");
   n = Convert.ToInt32(Console.ReadLine());    
 
   for (i = 1; i <= n; i++) {
      for (j = 0; j <= (ctr / 2); j++) 
	  {
         Console.Write("{0} ", alph++);
      }
      alph--;
      alph--;
 
      for (j = 0; j < (ctr / 2); j++) {
         Console.Write("{0} ", alph--);
      }
      ctr = ctr + 2;
      alph = 'A';
      Console.Write("\n");
   }
  } 
}

Sample Output:

Display the pattern like pyramid using the alphabet:                                                        
------------------------------------------------------                                                                                                 
Input the number of Letters (less than 26) in the Pyramid : 8                                               
A                                                                                                         
A B A                                                                                                       
A B C B A                                                                                                   
A B C D C B A                                                                                               
A B C D E D C B A                                                                                           
A B C D E F E D C B A                                                                                       
A B C D E F G F E D C B A                                                                                   
A B C D E F G H G F E D C B A 

Flowchart:

Flowchart: Display the pattern like pyramid using the alphabet

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to find the number and sum of all integer between 100 and 200 which are divisible by 9.
Next: Write a program in C# Sharp to convert a decimal number into binary without using an array.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming