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

Program for display pascal triangle in C language

Posted on December 6, 2019December 6, 2019

Table of Contents

  • Program for display pascal triangle in C language
    • Display pascal triangle using for loop
    • Display pascal triangle using while loop
    • Display pascal triangle using The do-while loop
    • Related

Program for display pascal triangle in C language

In this tutorial, we will discuss the Program for display pascal triangle in C language

In this post, we will learn how to display pascal triangle in C language using for, while and do-while loop

Program for display pascal triangle in C language
Pascal triangle

Display pascal triangle using for loop

Program 1

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

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

int main()
{
    int rows,i,j,space,count=1;
 printf("Enter the row for pascal triangle: ");
 scanf("%d",&rows);
 for(i=0; i<rows; i++){//outer for loop
   for(space=1; space<=rows-i; space++)
   printf("  ");
//first inner loop print space

   for(j=0; j<=i; j++){
   if(j==0 || i==0)
    count=1;
   else
     count=count*(i-j+1)/j;
     printf("%4d",count);

   }
   printf("\n");

}
getch();
    return 0;
}

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

Program for display pascal triangle in C language
output

 

Display pascal triangle using while loop

Program 2

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

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

int main()
{
    int rows,i,j,space,count=1;
 printf("Enter the row for pascal triangle: ");
 scanf("%d",&rows);
 i=0;
 while(i<rows){//outer vwhile loop
        space=1;
   while( space<=rows-i){
   printf("  "); //first inner loop print space
  space++;
   }
j=0;
   while( j<=i){
   if(j==0 || i==0)
    count=1;
   else
     count=count*(i-j+1)/j;
     printf("%4d",count);
      j++;

   }
   printf("\n");
i++;
}
getch();
    return 0;
}

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

Program for display pascal triangle in C language
Output

 

Display pascal triangle using The do-while loop

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

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

int main()
{
    int rows,i,j,space,count=1;
 printf("Enter the row for pascal triangle: ");
 scanf("%d",&rows);
 i=0;
do{
        space=1;
   do{
   printf("  ");  //first inner loop print space
  space++;
   }while( space<=rows-i);
j=0;
   do{
   if(j==0 || i==0)
    count=1;
   else
     count=count*(i-j+1)/j;
     printf("%4d",count);
      j++;

   }while( j<=i);
   printf("\n");
i++;
} while(i<rows);
getch();
    return 0;
}

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

Program for display pascal triangle in C language
Output

 

Suggested for you

For loop in C             while loop in C            Do while loop in C

Nested for loop in C    Nested while loop in C

if statement in C         Operator in C

 

Similar post

Java program to print pascal triangle

C++ program to print pascal triangle

C program to triangle number pattern

C program to pyramid triangle number pattern

 

 

 

Related

Recent Posts

  • Multiply two numbers in Java using scanner| 5 different ways
  • 5 different ways to Divide two numbers in Java using scanner
  • Learn 8 Ways to Subtract Two Numbers Using Methods in Java
  • 10 ways to subtract two numbers in Java
  • Java Code Examples – Multiply Two Numbers in 5 Easy Ways
  • How to Divide two numbers in Java| 5 different ways

tag

Addition (8) Array (38) C++ language (91) C language (98) c sharp (23) Division (8) Function (29) if else (32) Java language (108) JavaScript (5) loops (138) Multiply (8) Oop (2) patterns (66) PHP (13) Python Language (38) Subtraction (9) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2026 Code for Java c | Powered by SuperbThemes