Number pattern

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

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
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

Explanation of one dimensional array (Data structure)

Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…

2 days ago

Python program to calculate the sum of odd and even numbers in a list

Python program to calculate the sum of odd and even numbers in a list In…

5 days ago

Python code to Calculate sum of odd and even in a list

Python code to Calculate sum of odd and even in a list In this tutorial,…

1 week ago

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 weeks ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

1 month ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

2 months ago