Array

C# program to print pascal triangle number pattern

C# program to print the pascal triangle number pattern

In this tutorial, we will discuss the concept of the C# program to print the pascal triangle number pattern

In this topic, we are going to learn how  to write a program to print Pascal triangle patterns using numbers in the C# programming language

Here, we  use for, while, and do-while loops for printing pascal triangle

Pascal Triangle program

Display pascal pattern in C# using loops

understanding Pascal Triangle

C# Code to display  the pascal triangle using for loop

In this program, the user is asked to enter a number of rows and then it will show a pascal triangle number pattern using for loop in C# language

Program 1

// Print pascal triangle number pattern

using System;
namespace PascalTriane{
public class pascalPattern
{
    public static void Main(string[] args)
    {
        //decare variabes
        int rows,i,j,count=1;
        
        //Take input from user
        Console.Write ("Enter the numbers for rows: ");
        rows=Convert.ToInt32( Console.ReadLine());
        
        //move to new line
         Console.WriteLine (" ");
         
         //dispay the pattern
         for(i=1; i<=rows; i++){
             //initialize first coefficient
             count=1;
             
             //print space
             for(j=1; j<=rows-i; j++)
                 Console.Write (" ");
                 
                 //print number
                 for(j=1; j<=i; j++){
                 Console.Write (count+" ");
                 //calculate the next coefficient
                 count=count*(i-j)/j;
             }
              //move to new iine   
               Console.WriteLine (" ");
    }
      Console.ReadKey();
}
}
}

When the above code is executed, it produces the following result

Output 1

C# Code to display the pascal triangle using a while loop

In this program, the user is asked to enter a number of rows and then it will show a pascal triangle number pattern using a while loop in C# language

Program 2

// Print pascal triangle number pattern

using System;
namespace PascalTriane{
public class pascalPattern
{
    public static void Main(string[] args)
    {
        //decare variabes
        int rows,i,j,count=1;
        
        //Take input from user
        Console.Write ("Enter the numbers for rows: ");
        rows=Convert.ToInt32( Console.ReadLine());
        
        //move to new line
         Console.WriteLine (" ");
         
         //dispay the pattern
         i=1;
         while(i<=rows){
             //initialize first coefficient
             count=1;
             
             //print space
             j=1;
             while(j<=rows-i){
                 Console.Write (" ");
                 j++;
             }
                 //print number
                 j=1;
                 while(j<=i){
                 Console.Write (count+" ");
                 //calculate the next coefficient
                 count=count*(i-j)/j;
                  j++;
             }
              //move to new iine   
               Console.WriteLine (" ");
                i++;
    }
      Console.ReadKey();
}
}
}

When the above code is executed, it produces the following result

Output 2

Similar post

Java code to print pascal triangle

C code to print pascal triangle

C++ code to print pascal triangle

Java code to print pascal triangle using an array

Java program to print pascal triangle using array using user input

C program to print a pascal triangle using an array

C program to print pascal triangle using array using user input

Java program to pascal triangle number pattern using 2 D array

C program to pascal triangle number pattern using 2 D array

 

 

C code to Generate Pascal's triangle using 2 D array
Write a program to find first n prime numbers in Java
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

1 day ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago