In this C++ tutorial, you will learn how to find exponential of a given number using exp() function of cmath, with syntax and examples.

C exp

C++ exp() returns exponential (e) raised to given number (argument).

exp(x) = e^x

Syntax

The syntax of C++ exp() is

exp(x)

where

Parameter Description
x A double, float, long double, or any integral type value.

Returns

The return value depends on the type of value passed for parameter x.

The return value of exp(x) is

  • double if x is double or integral type.
  • float if x is float.
  • long double if x is long double.

The synopsis of exp() function is

double exp(double x);
float exp(float x);
long double exp(long double x);
double exp(T x); // for integral type argument values

exp() is a function of cmath library. Include cmath library in the program, if using exp() function.

Example

In this example, we read a value from user into variable x, and find exponential raised to this number x, using exp() function.

C++ Program

#include <iostream>
#include<cmath>
using namespace std;

int main() {
    double x;
    cout << "Enter a number : ";
    cin >> x;
    
    double result = exp(x);
    cout << "exp(" << x << ") : " << result << endl;
}

Output

Enter a number : 1
exp(1) : 2.71828
Program ended with exit code: 0
Enter a number : 0
exp(0) : 1
Program ended with exit code: 0
Enter a number : 10
exp(10) : 22026.5
Program ended with exit code: 0

Conclusion

In this C++ Tutorial, we learned the syntax of C++ exp(), and how to use this function to find exponential (e) raised to given number, with the help of examples.