Codeforcoding

C program to Generate Pascal triangle using 1 D array

C program to Generate Pascal triangle using 1 D array

In this tutorial, we will discuss the concept of the C program to Generate a Pascal triangle using a 1 D array

In this topic, we are going to learn how  to write a program to print Pascal triangle number patterns using a single 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 C 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[30],arrTemp[30],i,j,k,l, rows=5;

    arrTemp[0]=1;
    arr[0]=1;

    for(j=0; j<rows; j++)
        printf(" ");
    printf(" 1\n");

    for(i=1; i<rows; i++){
            for(j=0; j<i; j++)
              printf(" ");

            for(k=1; k<=rows; k++){
            arr[k]=arrTemp[k-1]+arrTemp[k];
    }
    arr[i]=1;
    for(l=0; l<=i; l++){
        printf("%3d",arr[l]);
        arrTemp[l]=arr[l];

    }
        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  while loop in the C  language according to the rows

Program 1

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

int main()
{
    int arr[30],arrTemp[30];
    //declare two one dim arrays
    int i,j,k,l, rowNo=4;
    //declare and initialize

    arrTemp[0]=1;
    arr[0]=1;
//initialize array elements
j=0;
    while(j<rowNo){
        printf(" ");
        j++;
    }
    printf(" 1\n");

    i=1;

    while(i<rowNo){
            for(j=0; j<i; j++)
              printf(" ");
k=1;
            while(k<=rowNo){
            arr[k]=arrTemp[k-1]+arrTemp[k];
            k++;
    }
    arr[i]=1;
    l=0;
    while(l<=i){
        printf("%3d",arr[l]);
        arrTemp[l]=arr[l];
 l++;
    }
        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 display a pascal triangle number pattern using  do-while loop in the C   language according to the rows

Program 1

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

int main()
{
    int arr[30],arrTemp[30];
    //declare two one dim arrays
    int i,j,k,l, rowNo=5;
    //declare and initialize

    arrTemp[0]=1;
    arr[0]=1;
//initialize array elements
j=0;
   do{
        printf(" ");
        j++;
    } while(j<rowNo);
    printf(" 1\n");

    i=1;

    do{
            for(j=0; j<i; j++)
              printf(" ");
k=1;
            while(k<=rowNo){
            arr[k]=arrTemp[k-1]+arrTemp[k];
            k++;
    }
    arr[i]=1;
    l=0;
   do{
        printf("%3d",arr[l]);
        arrTemp[l]=arr[l];
 l++;
    } while(l<=i);
        printf("\n");
i++;
}while(i<rowNo);
getch();
 return 0;
}


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

Output 3

 

 

Suggested for you

Data type and variable 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 array

Java program to print pascal triangle using array usin user input

 

 

C code to Alphabet triangle pattern using the do-while loop

C++ code to Alphabet triangle pattern using the do-while loop

Java code to Alphabet triangle pattern using the do-while loop

 

Alphabet  pattern in C language

Alphabet triangle pattern in C language using while loop

Alphabet  pattern in Java language

Alphabet triangle pattern in Java language using while loop

Alphabet  pattern in C++ language

 

Program to display pascal triangle  pattern in C language

 

Java code to generate pascal triangle using arrays with user input
C code to Generate Pascal triangle using 1 D array using user input
Exit mobile version