In this C++ tutorial, you will learn how to find the value of 2 raised to given number using exp2() function of cmath, with syntax and examples.
C++ exp2()
C++ exp2() returns the value of 2 raised to given number (argument).
exp2(x) = 2^x
Syntax
The syntax of C++ exp2() is
exp2(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 exp2(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 exp2() function is
double exp2(double x);
float exp2(float x);
long double exp2(long double x);
double exp2(T x); // for integral type argument values
exp2() is a function of cmath library. Include cmath library in the program, if using exp2() function.
Example
In this example, we read a value from user into variable x, and find the value of 2 raised to this number x, using exp2() function.
C++ Program
#include <iostream>
#include<cmath>
using namespace std;
int main() {
double x;
cout << "Enter a number : ";
cin >> x;
double result = exp2(x);
cout << "exp2(" << x << ") : " << result << endl;
}
Output
Enter a number : 1
exp2(1) : 2
Program ended with exit code: 0
Enter a number : 10
exp2(10) : 1024
Program ended with exit code: 0
Enter a number : -3
exp2(-3) : 0.125
Program ended with exit code: 0
Conclusion
In this C++ Tutorial, we learned the syntax of C++ exp2(), and how to use this function to find the value of 2 raised to given number, with the help of examples.