Array

C program to generate pascal triangle using Array

C program to generate pascal triangle using Array

In this tutorial, we will discuss the C program to generate a pascal triangle using an Array

In this post, we are going to learn how to display the pascal triangle pattern 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

in this program, the user declares and initializes some variables as integers and then the program will display the pascal triangle number pattern using for loop in C language

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

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

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

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

            for(k=1; k<=rowNo; 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

Program to display pascal triangle Using for loop with user input

Program 2

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 arrOne[30],arrTemp[30];//Declare single dim arrays
    int i,j,k,l, rowNum;//declare variables
    printf("Enter the Number o rows : ");
    //ask input
    scanf("%d",&rowNum);
//Reading the input
    arrTemp[0]=1;//initialize Single dim temp array index 0
    arrOne[0]=1;//initialize Single dim array index 0

    for(j=0; j<rowNum; j++)
        printf(" ");//print space
    printf(" 1\n");

    for(i=1; i<rowNum; i++){
            for(j=0; j<i; j++)
              printf(" ");
//print space
            for(k=1; k<=rowNum; k++){
            arrOne[k]=arrTemp[k-1]+arrTemp[k];
    }//print numbers
    arrOne[i]=1;
    for(l=0; l<=i; l++){
        printf("%3d",arrOne[l]);
        arrTemp[l]=arrOne[l];

    }
        printf("\n");
//move to next line
}
getch();
 return 0;
}

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

Enter the Number o rows :3

Output 2

 

 

Program to display pascal triangle Using while loop

Program 3

in this program , the user declares and initializes some variables as integer and then the program will display the pascal triangle number pattern using while loop in C language

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

int main()
{
    int arr[30],arrTemp[30];
    //declare two one dim arrays
    int i,j,k,l, numRow=4;
    //declare and initialize
//printf("Enter the number of rows: ");
//scanf("%d",&num_Row);
    arrTemp[0]=1;
    arr[0]=1;
//initialize array elements
j=0;
    while(j<numRow){
        printf(" ");
        j++;
    }
    printf(" 1\n");

    i=1;

    while(i<numRow){
            j=0;
            while(j<i){
              printf(" ");
              j++;
            }
k=1;
            while(k<=numRow){
            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 3

 

Program to display pascal triangle Using while loop with user input

Program 4

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

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

int main()
{
    int arr[30],arrTemp[30];
    //declare two one dim arrays
    int i,j,k,l, numRow;
    //declare and initialize
printf("Enter the value for the number of rows: ");
scanf("%d",&numRow);
    arrTemp[0]=1;
    arr[0]=1;
//initialize array elements
j=0;
    while(j<numRow){
        printf(" ");
        j++;
    }
    printf(" 1\n");

    i=1;

    while(i<numRow){
            j=0;
            while(j<i){
              printf(" ");
              j++;
            }
k=1;
            while(k<=numRow){
            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

Enter the value for the number of rows: 5

Output 4

 

 

 

Program to display pascal triangle Using do-while loop

Program 5

in this program , the user declares and initializes some variables as integers and then the program will display the pascal triangle number pattern using do-while loop in Java language

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

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

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

    i=1;

    do{
            for(j=0; j<i; j++)
              printf(" ");
k=1;
            while(k<=rowNum){
            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<rowNum);
getch();
 return 0;
}


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

Output 4

 

Program to display pascal triangle Using do-while loop with user input

Program 6

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

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

int main()
{
    int arr[30],arrTemp[30];
    //declare two one dim arrays
    int i,j,k,l, rowNo;
    //declare and variables
printf("Enter the value for the number of rows:  ");
//ask input from user
scanf("%d",&rowNo);
//read the input
    arrTemp[0]=1;
    arr[0]=1;
//initialize array elements as 1
j=0;
   do{
        printf(" ");
        j++;
    } while(j<rowNo);
    printf(" 1\n");

    i=1;

    do{//outter do-while 
            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{//inner do-while
        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

Enter the value for the number of rows: 5

Output 6

 

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 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

3 months ago

PHP Star triangle Pattern program

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

3 months 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.