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

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.