Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
C Sharp Exercise: print Pascal triangle pattern

C Sharp Exercise: print Pascal triangle pattern

Posted on October 8, 2022October 8, 2022

Table of Contents

  • C Sharp Exercise: print Pascal triangle pattern
    • Program to generate pascal’s triangle
      • Program to display pascal triangle Using for loop
      • Program to display pascal triangle Using while loop
    • Related

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,

Java program to print pascal triangle
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

C Sharp Exercise: print Pascal triangle pattern
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

 

 

Related

Recent Posts

  • Multiply two numbers in Java using scanner| 5 different ways
  • 5 different ways to Divide two numbers in Java using scanner
  • Learn 8 Ways to Subtract Two Numbers Using Methods in Java
  • 10 ways to subtract two numbers in Java
  • Java Code Examples – Multiply Two Numbers in 5 Easy Ways
  • How to Divide two numbers in Java| 5 different ways

tag

Addition (8) Array (38) C++ language (91) C language (98) c sharp (23) Division (8) Function (29) if else (32) Java language (108) JavaScript (5) loops (138) Multiply (8) Oop (2) patterns (66) PHP (13) Python Language (38) Subtraction (9) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2026 Code for Java c | Powered by SuperbThemes