Solutions to C Programming Challenges
1. Pascal's Triangle
Algorithm for Pascal's Triangle:
1. Start
2. Input the number of rows (n).
3. Loop through rows from 0 to n-1.
4. For each row, calculate and print the coefficients using:
- coeff = coeff * (row - col) / (col + 1) for col = 0 to row.
5. End
C Code:
#include <stdio.h>
void pascalTriangle(int n) {
for (int i = 0; i < n; i++) {
int coeff = 1;
for (int j = 0; j <= i; j++) {
printf("%d ", coeff);
coeff = coeff * (i - j) / (j + 1);
printf("\n");
int main() {
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
pascalTriangle(n);
return 0;
2. Series Summation: 1/1! + 1/2! + ... + 1/n!
Algorithm for Series Summation:
1. Start
2. Input the value of n.
3. Initialize sum = 0.
4. Loop through i from 1 to n:
- Calculate factorial of i.
- Add 1/factorial to sum.
5. Print the sum.
6. End
C Code:
#include <stdio.h>
double factorial(int n) {
if (n == 0 || n == 1)
return 1;
return n * factorial(n - 1);
double seriesSum(int n) {
double sum = 0;
for (int i = 1; i <= n; i++) {
sum += 1.0 / factorial(i);
}
return sum;
int main() {
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Sum of the series: %.6f\n", seriesSum(n));
return 0;