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

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago