Loop

C Program to print Pascal Triangle

C Program to print Pascal Triangle

In this tutorial, we will discuss the concept of  C Program to print Pascal Triangle

In this topic, we are going to learn how  to write a program to print Pascal triangle pattern using number in C programming language

Here, we  use for, while and do-while loops for printing pascal triangle

Program to print Pascal Triangle

Display pascal triangle in C using loops

understanding Pascal Triangle

C code display pascal triangle using for loop

In this program, the user is asked to enter number of rows and then it will display pascal triangle number pattern using for loop in C language

Program 1

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

int main()
{
    int rows,i,j,space,count=1;
    //variable declaration
 printf("Enter the No 0f rows: ");
 scanf("%d",&rows);
 //reading the input for rows
 for(i=0; i<rows; i++){
   for(space=1; space<=rows-i; space++)
   printf("  ");
//print space for left side
   for(j=0; j<=i; j++){
   if(j==0 || i==0)
    count=1;
   else
     count=count*(i-j+1)/j;
     printf("%4d",count);
//print start for pattern
   }
   printf("\n");

}
getch();
    return 0;
}

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

Program to print Pascal Triangle

 

C code display pascal triangle using while loop

In this program, the user is asked to enter number of rows and then it will display pascal triangle number pattern using while loop in C language

Program 2

#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);//Enter number of rows to print pascal triangle
 i=0;
 while(i<rows){//outer while loop  to print space for every line
        space=1;
   while( space<=rows-i){
   printf("  ");//print space
  space++;
   }
j=0;
   while( j<=i){//inner while loop for print pascal triangle
   if(j==0 || i==0)
    count=1;
   else
     count=count*(i-j+1)/j;
     printf("%4d",count);//print pascal triangle
      j++;

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

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

Program to print Pascal Triangle

 

C code display pascal triangle using do-while loop

In this program, the user is asked to enter number of rows and then it will display pascal triangle number pattern using do-while loop in C language

Program 3

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

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

   }while( j<=i);
   printf("\n");//move to next line
i++;
} while(i<rows);
getch();
    return 0;
}

 

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

Program to print Pascal Triangle

 

Suggested for you

Data type and variable in Java language

The operator in Java language

 

 

Similar post

Java code to print pascal triangle

C code to print pascal triangle

C++ code to print pascal triangle

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

 

C code to Alphabet triangle pattern using the do-while loop

C++ code to Alphabet triangle pattern using the do-while loop

Java code to Alphabet triangle pattern using the do-while loop

 

Alphabet  pattern in C language

Alphabet triangle pattern in C language using while loop

Alphabet  pattern in Java language

Alphabet triangle pattern in Java language using while loop

Alphabet  pattern in C++ language

Alphabet triangle pattern in C++ language using while loop

 

 

Program to print the Pascal Triangle in Java
Pascal Triangle program in C++ language
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

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago