Soru: Write a C program to display the sum of the series: 1 + 𝑥 +𝑥^2/2! +𝑥^3/3! +x^4/4! + ⋯𝑥^n/n!
Kod:
C:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x, n, i, j, sum =1;
float fac = 1, result =1, f;
printf("enter value of x: ");
scanf("%d", &x);
printf("enter value of n: ");
scanf("%d", &n);
for(i=0; i < n ; i++)
sum= sum * x;
for(j = 1; j <= n; j++)
{
fac = fac * j;
f = sum / fac;
result = result + f;
}
printf("The result is : %d", result);
return 0;
}
Soru: Write a C program to display the sum of the series: 1 + 𝑥 +𝑥^2/2! +𝑥^3/3! +x^4/4! + ⋯𝑥^n/n!
Kod:
C:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x, n, i, j, sum =1;
float fac = 1, result =1, f;
printf("enter value of x: ");
scanf("%d", &x);
printf("enter value of n: ");
scanf("%d", &n);
for(i=0; i < n ; i++)
sum= sum * x;
for(j = 1; j <= n; j++)
{
fac = fac * j;
f = sum / fac;
result = result + f;
}
printf("The result is : %d", result);
return 0;
}
#include <stdio.h>
// Function to calculate the factorial of a number
double factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
// Function to calculate the sum of the series
double calculateSeriesSum(int x, int n) {
double sum = 0;
for (int i = 0; i <= n; i++) {
sum += (double)pow(x, i) / factorial(i);
}
return sum;
}
int main() {
int x, n;
// Get user input for x and n
printf("Enter the value of x: ");
scanf("%d", &x);
printf("Enter the value of n: ");
scanf("%d", &n);
// Calculate and display the sum of the series
double result = calculateSeriesSum(x, n);
printf("Sum of the series is: %.4f\n", result);
return 0;
}