Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
C Sharp Exercise: print Pascal triangle pattern

C exercise: Pascal’s triangle pattern using 2D Array

Posted on October 8, 2022

Table of Contents

  • C exercise: Pascal’s triangle pattern using 2D Array
    • C program to print pascal triangle
      • Program to display pascal triangle Using for loop
      • Program to display pascal triangle Using while loop
      • Program to display pascal triangle Using a do-while loop

C exercise: Pascal’s triangle pattern using 2D Array

In this tutorial, we will discuss the title of the C exercise: Pascal’s triangle using a 2D Array

In this post, we are going to learn how to display the pascal triangle pattern using a 2D array in C language using for, while, and do-while loop

Java program to print pascal triangle
Pascal triangle

C program to print pascal triangle

Program to display pascal triangle Using for loop

Program 1

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 arr[50][50];
    //declare 2D array
    int i=0,j=0,count=0;
    //declare and initialize variables
      printf("Enter the number for rows: ");
      //Ask input from the user
    scanf("%d",&count);
    //Reading input
    for(i=0; i<count; i++){
         for(j=0; j<count-1-i; ++j)
                printf(" ");//print space
         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]);
         }    //print number
    printf("\n");
//move to new line
         }
         getch();
    return 0;
}

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

C exercise: Pascal's triangle using 2D Array
Output 1

 

Program to display pascal triangle Using while loop

Program 2

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

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

int main()
{
    int arr[50][50];
    //declare 2D array
    int i=0,j=0,count=0;
     //declare and initialize variables
      printf("Enter the number o rows: ");
       //Ask input from the user
    scanf("%d",&count);
    //Reading the input
    i=0;
    while(i<count){//outer while
        j=0;
         while(j<count-1-i){//inner while
                printf(" ");
                //print space
    ++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

C exercise: Pascal's triangle using 2D Array
Output 2

Program to display pascal triangle Using a do-while loop

Program 1

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

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

int main()
{
    int arr[50][50];//declare 2D array
    int i=0,j=0,count=0;
    //declare and initialize variables
      printf("Enter the number o rows: ");
       //Ask input from the user
    scanf("%d",&count);
    //Reading the input
    i=0;
    do{
        j=0;
         do{
                printf(" ");
    ++j;
         }while(j<=count-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<count);
         getch();
    return 0;
}

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

C exercise: Pascal's triangle using 2D Array
Output 3

 

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 print pascal triangle

C program to print pascal triangle using 1 D array

C++ program to print pascal triangle using 1 D array

Java program to print pascal triangle using 1 D array

Java program to print pascal triangle using 2 D array

Java program to triangle number pattern

Java program to triangle number pattern using while loop

Java program to pyramid triangle star pattern

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes