Accept a float value as input and print the same in its exponential form.
What is Scientific Notation?
Scientific notation (also called exponential notation) is a way to express very large or very small numbers in a compact format using powers of 10.
scientific
Manipulator
In C++, the scientific
manipulator from <iomanip>
forces output to use exponential notation.
scientific
is the opposite of fixed
!
fixed
→ Regular decimal notation (1234.567)scientific
→ Exponential notation (1.234567e+03)Original Number | fixed | scientific |
---|---|---|
1234.567 |
1234.567000 |
1.234567e+03 |
0.00123 |
0.001230 |
1.230000e-03 |
5000000 |
5000000.000000 |
5.000000e+06 |
0.0000007 |
0.000001 |
7.000000e-07 |
setprecision()
You can control the precision of the mantissa:
cout << scientific << setprecision(3) << 1234.567;
// Output: 1.235e+03 (3 digits after decimal)
scientific
shows 6 decimal places in the mantissa+
or -
)e+03
not e+3
)To go back to default formatting (auto-selection of fixed/scientific):
cout << defaultfloat; // Resets to default behavior
scientific
is persistent - it stays active for all
subsequent outputs until you change it with fixed
or defaultfloat
.
📚 Related Questions:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
long double f;
cin >> f;
// scientific: Forces exponential notation
// Format: [mantissa]e[+/-][exponent]
// Example: 1234.567 → 1.234567e+03
cout << scientific << f;
return 0;
}