I'm quite new to programming and just learned the basics of C.I was trying to build the pascal's triangle with C. But can't make it acute angled.
This is what I've done.
#include<stdio.h>
int main(void)
{
int n,s=1;
do
{
printf("give the value of n: \n");
scanf("%d",&n);
}
while(n<0);
for (int i=0; i<=n;i++)
{
printf("1 ");
for (int j=1;j<=i;j++)
{
s=s*(i-j+1)/(j);
printf("%d ",s);
}
printf("\n");
}
}
this gives a right angled triangle, like
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
but how to make it an acute angled one? like
1
1 1
1 2 1
1 3 3 1
Thank you.
printf()returns the number of characters printed. You could save all the lines as strings and then print them centered using the length of the last line. But what the heck, just compute the triangle twice. Then nothing advanced likemalloc()would be needed.Do it once just to get the length of the last line (you can use
snprintf()into a zero-length buffer to get that length), and then compute it again to print out the lines centered. For each line you can first usesnprintf()to get the length, and thenprintf()with a%*sin the format andn, "",in the arguments to precede it withnspaces. Ok, really three times, since you'll need to compute the length of each line again. Though you could save the lengths in a variable length array, which C permits.