C pattern

Program for display pascal triangle in C language

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

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

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

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

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

 

 

 

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.