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
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
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
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
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
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…