Codeforcoding

C code to Generate Pascal’s triangle using 2 D array

C code to Generate Pascal’s triangle using 2 D array

In this tutorial, we will discuss the concept of the C code to Generate Pascal’s triangle using a 2 D array

In this topic, we are going to learn how  to write a program to print Pascal triangle number patterns using a two dimension Array in the C  programming language

Here, we  use for, while, and do-while loops for printing pascal triangle

Program to print Pascal Triangle in Java
Print Pascal Triangle

Display the pascal triangle in Java using loops

Pascal Triangle in Java

C Code to display pascal triangle using for loop

In this program, the user declares and initializes integer variables, it will display a pascal triangle number pattern using for loop in the C language according to the rows

Program 1

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

int main()
{
    int arr[50][50];
    int i=0,j=0,num=0;
      printf("Enter the number o rows: ");
    scanf("%d",&num);
    for(i=0; i<num; i++){
         for(j=0; j<num-1-i; ++j)
                printf(" ");
         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]);
    }
    printf("\n");

         }
         getch();
    return 0;
}

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

Output 1

 

C Code to display pascal triangle using while loop

In this program, the user declares and initializes integer variables, it will display a pascal triangle number pattern using a while loop in the C  language according to the rows

Program 1

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

int main()
{
    int arr[50][50];
    int i=0,j=0,num=0;
      printf("Enter the number o rows: ");
    scanf("%d",&num);
    i=0;
    while(i<num){
        j=0;
         while(j<num-1-i){
                printf(" ");
    ++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

 

C Code to display pascal triangle using do-while loop

In this program, the user declares and initializes integer variables, it will show a pascal triangle number pattern using a do-while loop in the C  language according to the rows

Program 3

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

int main()
{
    int arr[50][50];
    int i=0,j=0,num=0;
      printf("Enter the number o rows: ");
    scanf("%d",&num);
    i=0;
    do{
        j=0;
         do{
                printf(" ");
    ++j;
         }while(j<=num-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<num);
         getch();
    return 0;
}

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

Output 3

Suggested for you

Data type  in C language

The operator in the C language

For loop in C language

While loop in C language

Do-while loop in C language

 

Similar post

Java program to print pascal triangle

C program to print pascal triangle

C++ program to print pascal triangle

Java program to print pascal triangle using an array

Java program to print pascal triangle using array using user input

C program to print a pascal triangle using an array

C program to print pascal triangle using array using user input

Java program to pascal triangle number pattern using 2 D array

C program to pascal triangle number pattern using 2 D array

Program to Generate Pascal's triangle using 2 D array in Java
C# program to print pascal triangle number pattern
Exit mobile version