Codeforcoding

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 in C++ language
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
Exit mobile version