使用函数的帕斯卡三角形

Pascal triangle using a function

well I've got it how to construct the pascal triangle and the code below is flawless but...in this code i am making the 1 to appear in first row by creating a new for loop especially for it... is there a way to generate pascal triangle without using an exclusive for loop for the 1 to appear in first... any help is much appreciated :)

//pascal triangle with ncr function
        #include <stdio.h>
        #include <conio.h>
        int ncr(int i,int j);
        int main()
        {
            int i,j,v,n,f,s;
            printf("Enter the number of rows required\n");
            scanf("%d",&n);
            f=n;
     //this is what i am exclusively using for printing 1 in 1st row
            for(;f>0;f--)
            {
                printf(" ");
            }
            printf("1\n");
     //is there a way to generate the above 1 using only the below for loop
            for(i=1;i<=n;i++)
            {
                for(s=n-i;s>0;s--)
                {
                    printf(" ");
                }
                for(j=0;j<=i;j++)
                {
                    v=ncr(i,j);
                    printf("%d ",v);
                }
                printf("\n");
            }
        }
        int ncr(int i,int j)
        {
            int k;
            float ans=1;
            for(;j>=1;j--)
                {
                    ans=((ans*i)/j);
                    i--;
                }
                k=ans;
                return(k);
        }

如果仔细观察,您会注意到 ncr 函数是在 方法 中定义的。将 ncr 的实现移到 main.

之外

此外,@BLUEPIXY 注意到,您对 ncr 的实现有多余的 ;:

int ncr(int i,int j); //<<right here
{
//...

编辑 第二个问题的解决方案(参见Pascal's Triangle on Wikipedia

三角形的"first"行实际上是第零行。您的外部循环以 i = 1 开头,因此 "first" 行包含 1C01C1。 "first" 或第零行实际上应该只包含 0C0。新循环:

//notice how the i here has changed to 0
for(i=0;i<=n;i++)
{
    for(s=n-i;s>0;s--)
    {
        printf(" ");
    }
    for(j=0;j<=i;j++)
    {
        v=ncr(i,j);
        printf("%d ",v);
    }
    printf("\n");
}