- On
- By
- 0 Comment
- Categories: do-while, for loop, Loop, Number pattern, While 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
Display pascal triangle in C using loops
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
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
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
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