Array

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

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 and variable in Java language

The operator in the Java 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
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

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago