Create a Prime Number Pyramid in C#

In this article, I am going to create a prime number pyramid in C# Language. You should learn the concepts and practice them by solving such questions. It is an important question of technical interview rounds. To solve this problem, you must have a basic knowledge of the C# concepts.

Program Outline

Question: Write a Program to create a prime number pyramid in C# Programming?


STEP 1. Each row is iterated in the outer for loop.
STEP 2. For printing the spaces and digits, we utilize the two counters count and count1.
STEP 3. The formula (rows-i)+1 is used in the inner for loop to print the needed spaces, where rows is the total number of rows and are the current row number.
STEP 4. The numbers are printed in the while loop, with 2 * I – 1 indicating the number of items in each row.

Program to create prime number pyramid in C#

Here’s the C# program to create a prime number pyramid in C#:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
namespace ConsoleApplication59 {  
    class Program {  
        static void Main(string[] args) {  
            int numberoflayer = 6, Space, Number;  
            Console.WriteLine("Create Pyramid in C# Language");  
            for (int i = 1; i <= numberoflayer; i++)  
                for (Space = 1; Space <= (numberoflayer - i); Space++) 
                    Console.Write(" ");  
                for (Number = 1; Number <= i; Number++)  
                for (Number = (i - 1); Number >= 1; Number--)

Output of Program

Here’s the output of the above program:

Print pyramid

Leave a Reply

Your email address will not be published. Required fields are marked *