Write a C program to generate Pascal’s triangle
.
Algorithm:
Step 1: Start Step 2: Read p value as integer Step 3: while(q<p) begin for r:40 - 3 * q to 0 decrement r by 1 for x: 0 to q increment x by 1 begin if((x == 0) || (q == 0)) binom = 1; else binom = (binom * (q - x + 1)) / x; print binom end ++q; end Step 4: Stop
Program:
#include <stdio.h> #include <conio.h> long factorial(int); void main() { int i, n, c; clrscr(); printf("Enter the number of rows\n"); scanf("%d",&n); for (i = 0; i < n; i++) { for (c = 0; c <= (n - i - 2); c++) { printf(" "); } for (c = 0 ; c <= i; c++) { printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c))); } printf("\n"); } getch(); } long factorial(int n) { int c; long result = 1; for (c = 1; c <= n; c++) { result = result*c; } return result; }
Input & Output:
Enter the number of rows 5 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
-
UpdatedDec 25, 2014
-
Views14,874
You May Like
Write a C program to find the roots of a quadratic equation
.
Write a C program to count the lines, words and characters in a given text.
Write a C program to find the sum of individual digits of a positive integer.
Write a C program, which takes two integer operands and one operator from the user, performs the operation and then prints the result. (Consider the operators +,-,*, /, % and use switch statement
)
Write a C program to generate all the prime numbers
between 1 and n, where n is a value supplied by the user.
Write C programs that use both recursive
and non-recursive functions
- To find the
factorial
of a given integer. - To find the
GCD
(greatest common divisor) of two given integers.