Program #39
Description:
Print Pascal Triangle using C Program
/*
C Program full source code for Pascal Triangle
Pascal Triangle using C program
Coding for Pascal Triangle
*/
#include <stdio.h>
#include <stdlib.h>
/* Global Declaration */
int n, x[25][25];
int main()
{
/* Get Input value from User i.e. N */
printf("Please enter the N value:\n");
scanf("%d", &n);
/* Local Prototype Declaration */
void validateInput(void);
void intializeDefaultValues(void);
void fillRemainingValues(void);
void printResultMatrixFormat(void);
void printResultTriangleFormat(void);
validateInput();
intializeDefaultValues();
fillRemainingValues();
printResultMatrixFormat();
printResultTriangleFormat();
return 0;
}
/*
Function #1
-----------
Validate the Input value i.e N
*/
void validateInput() {
if (n<1 || n > 25) {
printf("Input is beyond this program Assumption !! \nPlease enter input value {1 to 25}\n");
exit(0);
}
}
/*
Function #2
-----------
Initialize default values
*/
void intializeDefaultValues(){
int i;
/* Fill default values in Row wise */
for (i=1; i<=n; i++){
x[i][1] = 1;
}
/* Fill default values in Column wise */
for (i=1; i<=n; i++){
x[1][i] = 1;
}
/*
Note:
In above code, we can use single FOR LOOP to initialize the Row/Column wise values.
Just making it simplified; It is your wish to simply this code further.
*/
}
/*
Function #3
-----------
Fill remaining values
*/
void fillRemainingValues(){
int i, j;
for(i=2; i<=n; i++){
for (j=2; j<=n; j++){
/*
Skip the x[i][j] position Summation
if (i-1) is zero (or)
if (j-1) is zero
*/
if ( ((i-1) == 0) || ((j-1) == 0)){
break;
} else {
/* Summation */
x[i][j] = x[i][j-1] + x[i-1][j];
}
}
}
}
/*
Function #4
-----------
Print Result in Matrix Format
*/
void printResultMatrixFormat(){
int i,j;
printf("\n\n");
for (i=1; i<=n+1; i++){
for (j=1; j<=n-i+1; j++){
printf("%5d",x[i][j]);
}
printf("\n");
}
}
/*
Function #5
-----------
Print Result in Triangle Format
*/
void printResultTriangleFormat(){
int i,j,k;
printf("\n\n");
for (i=1; i<=n; i++){
/*
Identify the Row position
To Print the result in Diagonal wise!!
*/
k=i;
for (j=1; j<=i; j++){
printf("%5d",x[k][j]);
/* Move towards Diagonally Upside */
k--;
/* Stop the Movement if it reaches 1st Row */
if(k==0){
/* Move Cursor to Next Row */
printf("\n");
/* Move to Next Row */
break;
}
}
printf("\n");
}
}
Sample Output using Input value as 5:
Sample Output using Input value as 10:
Description:
Print Pascal Triangle using C Program
/*
C Program full source code for Pascal Triangle
Pascal Triangle using C program
Coding for Pascal Triangle
*/
#include <stdio.h>
#include <stdlib.h>
/* Global Declaration */
int n, x[25][25];
int main()
{
/* Get Input value from User i.e. N */
printf("Please enter the N value:\n");
scanf("%d", &n);
/* Local Prototype Declaration */
void validateInput(void);
void intializeDefaultValues(void);
void fillRemainingValues(void);
void printResultMatrixFormat(void);
void printResultTriangleFormat(void);
validateInput();
intializeDefaultValues();
fillRemainingValues();
printResultMatrixFormat();
printResultTriangleFormat();
return 0;
}
/*
Function #1
-----------
Validate the Input value i.e N
*/
void validateInput() {
if (n<1 || n > 25) {
printf("Input is beyond this program Assumption !! \nPlease enter input value {1 to 25}\n");
exit(0);
}
}
/*
Function #2
-----------
Initialize default values
*/
void intializeDefaultValues(){
int i;
/* Fill default values in Row wise */
for (i=1; i<=n; i++){
x[i][1] = 1;
}
/* Fill default values in Column wise */
for (i=1; i<=n; i++){
x[1][i] = 1;
}
/*
Note:
In above code, we can use single FOR LOOP to initialize the Row/Column wise values.
Just making it simplified; It is your wish to simply this code further.
*/
}
/*
Function #3
-----------
Fill remaining values
*/
void fillRemainingValues(){
int i, j;
for(i=2; i<=n; i++){
for (j=2; j<=n; j++){
/*
Skip the x[i][j] position Summation
if (i-1) is zero (or)
if (j-1) is zero
*/
if ( ((i-1) == 0) || ((j-1) == 0)){
break;
} else {
/* Summation */
x[i][j] = x[i][j-1] + x[i-1][j];
}
}
}
}
/*
Function #4
-----------
Print Result in Matrix Format
*/
void printResultMatrixFormat(){
int i,j;
printf("\n\n");
for (i=1; i<=n+1; i++){
for (j=1; j<=n-i+1; j++){
printf("%5d",x[i][j]);
}
printf("\n");
}
}
/*
Function #5
-----------
Print Result in Triangle Format
*/
void printResultTriangleFormat(){
int i,j,k;
printf("\n\n");
for (i=1; i<=n; i++){
/*
Identify the Row position
To Print the result in Diagonal wise!!
*/
k=i;
for (j=1; j<=i; j++){
printf("%5d",x[k][j]);
/* Move towards Diagonally Upside */
k--;
/* Stop the Movement if it reaches 1st Row */
if(k==0){
/* Move Cursor to Next Row */
printf("\n");
/* Move to Next Row */
break;
}
}
printf("\n");
}
}
Sample Output using Input value as 5:
Sample Output using Input value as 10:
No comments:
Post a Comment