C PROGRAM TO GENERATE PASCAL TRIANGLE AND PYRAMID OF NUMBERS
A)
Pascal triangle
of numbers:
#include<stdio.h>
#include<conio.h> int main()
{
int rows,k=1,space,i,j; clrscr();
printf("\nEnter number
of rows: ");
scanf("%d",&rows); for(i=0;i<rows;i++)
{
for(space=1; space <= rows-i;
space++) printf(" ");
for(j=0;j<=i;j++)
{
if(j == 0||i == 0) k=1;
else
k=k*(i-j+1)/j; printf("%4d",k);
}
printf("\n");
}
getch(); return 0;
}
Output:
Enter
no. of rows: 6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
B)
Pyramid of numbers.
#include<stdio.h> #include<conio.h> #include<math.h>
int main()
{
int rows,space,i,j; clrscr();
printf("\nEnter number
of rows: ");// enter a number for generating the pyramid
scanf("%d",&rows);
for(i=0; i<rows; i++) // outer loop for
displaying rows
{
for(space=1; space <= rows-i; space++) // space for each and every element
printf(" ");
for(j=0-i; j <= i; j++) //inner loop for displaying the pyramid of numbers
{
printf("%2d",abs(j)); // prints the value
}
printf("\n"); // every line in different row
}
getch();
return 0;
}
Output:
Enter no. of
rows: 5
1
2 3 2
3 4 5 4 3
4 5 6 7 6
5 4
5 6 7 8 9
8 7 6 5
Comments
Post a Comment