← Back

17. Scientific Notation Output

Easy

📝 Problem

Accept a float value as input and print the same in its exponential form.

Input Format:
Read a Float number
Output Format:
Exponential form of the float value
Constraints:
2.3E-308 <=N< =1.7E+308
🔬 Understanding Scientific Notation

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.

Format Breakdown:
1.234567e+03
Mantissa:
1.234567 (the significant digits)
Exponent:
+03 (power of 10)
Meaning:
1.234567 × 10³ = 1234.567

🎯 The scientific Manipulator

In C++, the scientific manipulator from <iomanip> forces output to use exponential notation.

Key Point: scientific is the opposite of fixed!
  • fixed → Regular decimal notation (1234.567)
  • scientific → Exponential notation (1.234567e+03)

📊 Comparison: fixed vs scientific

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

💡 When to Use Scientific Notation?

  • Astronomy: Distance to stars (9.461e+15 meters)
  • Physics: Avogadro's number (6.022e+23)
  • Chemistry: Atomic masses (1.67e-27 kg)
  • Engineering: Micro/nano measurements (5.2e-09 meters)
  • Computing: Very large/small calculations

🔧 Combining with setprecision()

You can control the precision of the mantissa:

cout << scientific << setprecision(3) << 1234.567;
// Output: 1.235e+03 (3 digits after decimal)

⚙️ Default Behavior

  • By default, scientific shows 6 decimal places in the mantissa
  • Always shows sign of exponent (+ or -)
  • Exponent is at least 2 digits (e+03 not e+3)
  • The mantissa is always normalized: 1.0 ≤ mantissa < 10.0

🔄 Reverting to Default

To go back to default formatting (auto-selection of fixed/scientific):

cout << defaultfloat;  // Resets to default behavior
Remember: scientific is persistent - it stays active for all subsequent outputs until you change it with fixed or defaultfloat.

📚 Related Questions:

Examples

Sample Input 1:
Input:
1234.567
Sample Output 1:
Output:
1.234567e+03
Meaning: 1.234567 × 10³ = 1234.567
More Examples:
Input: 0.00567
Output: 5.670000e-03
Input: 9500000
Output: 9.500000e+06
Input: 0.0000042
Output: 4.200000e-06
Solution.cpp
#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;
}