pascals triangle using user-define function use. Not working !
-
it works fine until the 3 column but in the 4th column it doesn't.
#include <conio.h>
#include <stdio.h>long int factorial(int num);
int main(){
int row, col\_1, col\_2, i, j; long int k; printf("Enter the number of rows: \\n"); scanf("%d", &row); col\_1 = row-2; for(i=0; i<=row-1; i++){ for(j=0; j<=col\_1; j++){ printf(" "); } col\_1--; for(col\_2=0; col\_2<=i; col\_2++){ k = factorial(i)/( factorial(i-col\_2) \* factorial(col\_2) ) ; printf(" %ld ", k); } printf("\\n"); } return 0;
}
long int factorial(int num){
int total, i; if(num==0 || num==1){ total=1; }else{ total = 1; for(i=1; i<=num; i++){ total = total\*num; num--; } } return total;
}
-
it works fine until the 3 column but in the 4th column it doesn't.
#include <conio.h>
#include <stdio.h>long int factorial(int num);
int main(){
int row, col\_1, col\_2, i, j; long int k; printf("Enter the number of rows: \\n"); scanf("%d", &row); col\_1 = row-2; for(i=0; i<=row-1; i++){ for(j=0; j<=col\_1; j++){ printf(" "); } col\_1--; for(col\_2=0; col\_2<=i; col\_2++){ k = factorial(i)/( factorial(i-col\_2) \* factorial(col\_2) ) ; printf(" %ld ", k); } printf("\\n"); } return 0;
}
long int factorial(int num){
int total, i; if(num==0 || num==1){ total=1; }else{ total = 1; for(i=1; i<=num; i++){ total = total\*num; num--; } } return total;
}
And did you try to debug your code?
-
it works fine until the 3 column but in the 4th column it doesn't.
#include <conio.h>
#include <stdio.h>long int factorial(int num);
int main(){
int row, col\_1, col\_2, i, j; long int k; printf("Enter the number of rows: \\n"); scanf("%d", &row); col\_1 = row-2; for(i=0; i<=row-1; i++){ for(j=0; j<=col\_1; j++){ printf(" "); } col\_1--; for(col\_2=0; col\_2<=i; col\_2++){ k = factorial(i)/( factorial(i-col\_2) \* factorial(col\_2) ) ; printf(" %ld ", k); } printf("\\n"); } return 0;
}
long int factorial(int num){
int total, i; if(num==0 || num==1){ total=1; }else{ total = 1; for(i=1; i<=num; i++){ total = total\*num; num--; } } return total;
}
-
it works fine until the 3 column but in the 4th column it doesn't.
#include <conio.h>
#include <stdio.h>long int factorial(int num);
int main(){
int row, col\_1, col\_2, i, j; long int k; printf("Enter the number of rows: \\n"); scanf("%d", &row); col\_1 = row-2; for(i=0; i<=row-1; i++){ for(j=0; j<=col\_1; j++){ printf(" "); } col\_1--; for(col\_2=0; col\_2<=i; col\_2++){ k = factorial(i)/( factorial(i-col\_2) \* factorial(col\_2) ) ; printf(" %ld ", k); } printf("\\n"); } return 0;
}
long int factorial(int num){
int total, i; if(num==0 || num==1){ total=1; }else{ total = 1; for(i=1; i<=num; i++){ total = total\*num; num--; } } return total;
}
-
Your
factorial
function is flawed (a decrementingnum
as stop condition fori
is really a bad choice), try replacing it withlong int factorial(int num)
{
long int fact = 1;
int i;
for (i=2; i<=num; ++i)
fact *= i;return fact;
}