C Language "Exponential Series Example for loop" program with output
#include <stdio.h>
double power(double x, int n) { double result = 1.0; for (int i = 0; i < n; i++) { result *= x; } return result; } int factorial(int n) { int fact = 1; for (int i = 1; i <= n; i++) { fact *= i; } return fact; } int main() { double x; int n; printf("Enter the value of x: "); scanf("%lf", &x); printf("Enter the number of terms in the series: "); scanf("%d", &n); double sum = 1.0; // The first term in the series for (int i = 1; i < n; i++) { sum += power(x, i) / factorial(i); } printf("e^%.2lf = %.6lf\n", x, sum); return 0; }Output:
Enter the value of x: 2
Enter the number of terms in the series: 10
e^2.00 = 7.388712
Comments
Post a Comment