Array

C exercise: Pascal’s triangle pattern using 2D Array

C exercise: Pascal’s triangle pattern using 2D Array

In this tutorial, we will discuss the title of the C exercise: Pascal’s triangle using a 2D Array

In this post, we are going to learn how to display the pascal triangle pattern using a 2D array in C language using for, while, and do-while loop

Pascal triangle

C program to print pascal triangle

Program to display pascal triangle Using for loop

Program 1

This program allows the user to enter the number of rows, and it will display the pascal triangle number pattern using for loop in C language according to the rows

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int arr[50][50];
    //declare 2D array
    int i=0,j=0,count=0;
    //declare and initialize variables
      printf("Enter the number for rows: ");
      //Ask input from the user
    scanf("%d",&count);
    //Reading input
    for(i=0; i<count; i++){
         for(j=0; j<count-1-i; ++j)
                printf(" ");//print space
         for(j=0; j<=i; ++j){
                if(j==0 || j==i)
                arr[i][j]=1;
         else
            arr[i][j]=arr[i-1][j-1]+arr[i-1][j];
                printf("%d ", arr[i][j]);
         }    //print number
    printf("\n");
//move to new line
         }
         getch();
    return 0;
}

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

Output 1

 

Program to display pascal triangle Using while loop

Program 2

This program allows the user to enter the number of rows, and it will display the pascal triangle number pattern using a while loop in C language according to the rows

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int arr[50][50];
    //declare 2D array
    int i=0,j=0,count=0;
     //declare and initialize variables
      printf("Enter the number o rows: ");
       //Ask input from the user
    scanf("%d",&count);
    //Reading the input
    i=0;
    while(i<count){//outer while
        j=0;
         while(j<count-1-i){//inner while
                printf(" ");
                //print space
    ++j;
         }
         j=0;
         while(j<=i){
                if(j==0 || j==i)
                arr[i][j]=1;
         else
            arr[i][j]=arr[i-1][j-1]+arr[i-1][j];
                printf("%d ", arr[i][j]);
                ++j;
    }
    printf("\n");
    i++;

         }
         getch();
    return 0;
}

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

Output 2

Program to display pascal triangle Using a do-while loop

Program 1

This program allows the user to enter the number of rows, and it will display the pascal triangle number pattern using a do-while loop in C language according to the rows

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int arr[50][50];//declare 2D array
    int i=0,j=0,count=0;
    //declare and initialize variables
      printf("Enter the number o rows: ");
       //Ask input from the user
    scanf("%d",&count);
    //Reading the input
    i=0;
    do{
        j=0;
         do{
                printf(" ");
    ++j;
         }while(j<=count-1-i);
         j=0;
         do{
                if(j==0 || j==i)
                arr[i][j]=1;
         else
            arr[i][j]=arr[i-1][j-1]+arr[i-1][j];
                printf("%d ", arr[i][j]);
                ++j;
    }while(j<=i);
    printf("\n");
    i++;

         }while(i<count);
         getch();
    return 0;
}

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

Output 3

 

Suggested for you

For loop in C language               while loop in C language             Do while loop in C language

Nested for loop in C language     Nested while loop in C  language

if statement in C language         Operator in C language

 

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

4 weeks ago

PHP Star triangle Pattern program

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

4 weeks 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.