C program to print Pascal triangle up to given number rows

description: this program is find out perfect number  using function
</code>: Check number is perfect number or not
/* C program to print Pascal triangle up to given number rows   */
 
#include <stdio.h>
 
long factorial(int p);
 
int main()
{
    int p, q, number, r;
    long cal;
 
    printf("Enter number of rows :");
    scanf("%d", &number);
 
    for(p=0; p<=number; p++)
    {
        //here main logic is print two spaces
        for(r=p; r<=number; r++)
            printf("%2c", ' '); // 2c means two space
 
        for(q=0; q<=p; q++)
        {
   //calling factorial function
            cal = factorial(p)/( factorial(q) * factorial(p-q));
            printf("%4ld", cal);
        }
 
        printf("\n");
    }
 
    return 0;
}
 
//find factorial number
long factorial(int p)
{
    long f = 1;
    while(p>0)
    {
        f *= p;
        p--;
    }
 
    return f;
}
 
output:

Explanation:-
first define factorial(int n) fuction and start main() function then declare variable p, q, number, and r.
after declare variable get input from user as numbers of row. Repeate loop user input time that store in int number variable and start inner loop r=p and r<number times and start one more inner loop q that is inner loop of r loop.
here repeat loop q<=p times and coll factorial function cal = factorial(p)/( factorial(q) * factorial(p-q)) store value in long cal variable and print it.
printf("%4ld", cal)  close most inner loop and print  new line using printf(''\n")  and close it.
return 0 because  main() return type is int and close it.

In long factorial(int p) function define and initialize variable logn f =1     and repeate while loop
p>0 and in loop multiply variable f * p and minus one p
 while(p>0)
    {
        f *= p;
        p--;
    }

LANGUAGE