Exponential() in C
×


Exponential() in C

40

The exponential function in C calculates the result of raising a given base to a specified exponent.

 It can be implemented using the standard library function pow() or through iterative multiplication.

Syntax of Exponential function in C:

double pow(double base, double exponent);

Parameters:

base: The base number.

exponent: The power to raise the base to.


Return Value:

The function returns a double value which represents the result of base raised to the power of exponent.


Implementation:

1Custom Exponential Function: Implementing your version of the exponential function without using the exp() function from the math library.

// prgoram for Exponential Function in C
#include<stdio.h> 

double my_exp(double x) {
    double result = 1.0;
    double term = 1.0;
    int i;
    for (i = 1; i <= 10; ++i) {
        term *= x / i;
        result += term;
    }
    return result;
}

int main() {
    double x = 2.0;
    double result = my_exp(x);
    printf("e^%.2f = %.6f\n", x, result);
    return 0;
}

Output:

e^2.00 = 7.388995

2Employing a series expansion method to estimate the exponential function.

// Program for Exponential in C
#include<stdio.h> 

double exp_series(double x, int n) {
    double result = 1.0;
    double term = 1.0;
    int i;
    for (i = 1; i <= n; ++i) {
        term *= x / i;
        result += term;
    }
    return result;
}

int main() {
    double x = 2.0;
    double result = exp_series(x, 10);
    printf("The exponential of %.2f (approximated using 10 terms) is %.6f\n", x, result);
    return 0;
}

Output:

The exponential of 2.00 (approximated using 10 terms) is 7.388995

3Interactive Exponential Calculator: A program that takes input from the user and calculates the exponential value.

// Program for Exponential in C
#include<stdio.h>
#include<stdlib.h>
#include<math.h> 

int main() {
    double x;
    printf("Enter the value of x: ");
    scanf("%lf", &x);
    double result = exp(x);
   printf("The exponential of %.2f (approximated using 10 terms) is %.6f\n", x, result);

    return 0;
}		

Output:

Enter the value of x: 4
The exponential of 4.00 (approximated using 10 terms) is 54.598150

4Program of Exponential using Library function pow():

// program for Exponential using library function pow()
#include<stdio.h> 
#include<math.h> 

int main() {
    double base = 2.0; // Base of the exponential function
    double exponent = 3.0; // Exponent of the exponential function
    double result;

    // Calculate the exponential using pow() function
    result = pow(base, exponent);

    // Print the result
    printf("Exponential of %.2f raised to the power %.2f is %.2f\n", base, exponent, result);

    return 0;
}

Output:

exponential of 2.00 raised to the power 3.00 is 8.00


Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments