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

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,…

11 months 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…

11 months ago

Write temperature conversion program: Fahrenheit into Celsius

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

11 months 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…

11 months ago

Function/method to convert Celsius into Fahrenheit -Entered by user

Function/method to convert Celsius into Fahrenheit -Entered by user In this article, we will discuss…

11 months ago

Temperature conversion: Celsius into Fahrenheit using function or method

Temperature conversion: Celsius into Fahrenheit using a function or method In this article, we will…

11 months ago

This website uses cookies.