Array

C Sharp Exercise: print Pascal triangle pattern

C Sharp Exercise: print Pascal triangle pattern

In this tutorial, we will discuss the title of C Sharp Exercise: print Pascal triangle pattern

In this post, we are going to learn how to display the pascal triangle number pattern in C# language using for and while loop,

Pascal triangle

Program to generate pascal’s triangle

Program to display pascal triangle Using for loop

Program 1

The program allows the user to enter the number of rows and then the program will show the pascal triangle number pattern using for loop in the C# programming language according to the rows

//Print pascal triangle number pattern
//using for loop
using System;
namespace PascalTriangle{
public class pascalPatternFor
{
    public static void Main(string[] args)
    {
        //decare variabes as integers
        int rowNo,i,j,num=1;
        
        //Take input from user
        Console.Write ("Enter the numbers To rows: ");
        rowNo=Convert.ToInt32( Console.ReadLine());
        //reading the input rom user
    
        //move to new line
         Console.WriteLine (" ");
         
         //dispay the pattern
         for(i=1; i<=rowNo; i++){
             //initialize first coefficient
             num=1;
             
             //print space
             for(j=1; j<=rowNo-i; j++)
                 Console.Write (" ");
                 
                 //print number 
                 for(j=1; j<=i; j++){
                 Console.Write (num+" ");
         //print number with spaces
                 //calculate the next coefficient
                 num=num*(i-j)/j;
             }
              //move to new iine   
               Console.WriteLine (" ");
    }
      Console.ReadKey();
}
}
}

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

Output 1

 

Program to display pascal triangle Using while loop

Program 1

The program allows the user to enter the number of rows and then the program will show the pascal triangle number pattern using a while loop in the C# programming language

//Print pascal triangle number pattern
//using for loop
using System;
namespace PascalTriangle{
public class pascalPatternFor
{
    public static void Main(string[] args)
    {
        //decare variabes as integers
        int rowNo,i,j,num=1;
        
        //Take input from user
        Console.Write ("Enter the numbers To rows: ");
        rowNo=Convert.ToInt32( Console.ReadLine());
        //reading the input rom user
    
        //move to new line
         Console.WriteLine (" ");
         
         //dispay the pattern
         i=1; 
         while(i<=rowNo){
             //initialize first coefficient
             num=1;
             
             j=1;
             //print space
             while(j<=rowNo-i){
                 Console.Write (" ");
                  j++;
             }
                 //print number 
                 j=1;
                 while(j<=i){
                 Console.Write (num+" ");
         //print number with spaces
                 //calculate the next coefficient
                 num=num*(i-j)/j;
                 j++;
             }
              //move to new iine   
               Console.WriteLine (" ");
                i++;
    }
      Console.ReadKey();
}
}
}

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

 

 

Similar post

C program to print pascal triangle

C++ program to print pascal triangle

Java program to print pascal triangle

C program to print pascal triangle using 1 D array

C++ program to print pascal triangle using 1 D array

Java program to print pascal triangle using 1 D array

Java program to print pascal triangle using 2 D array

Java program to triangle number pattern

Java program to triangle number pattern using while loop

Java program to pyramid triangle star pattern

 

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.