Header Ads Widget

Ticker

6/recent/ticker-posts

How to print a double or floating point number in scientific notation and fixed notation

 Here in this article we are going to print a double number in scientific as well as fixed notation . There is a keyword fixed . By using this keyword we can easily print a number in fixed notation and for the scientific notation we can use scientific keyword. These are predefined keywords which are defined in iostream library file. We can also fix the number after point (.) in the fixed notation. for this we can use precision(x) ,here value of x denote how many numbers we want after point  .

example :-
#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    double a=123.4589;
    double b=1.0e6;
    double c=1256.32;
    
    // fixed notation
    cout<<"The fixed notation of the numbers are"<<endl;
    cout<<fixed<<a<<" "<<b<<" "<<c<<endl;
    
    // scientific notation
    cout<<"The scientific notation of the numbers are"<<endl;
    cout<<scientific<<a<<" "<<b<<" "<<c<<endl;
    
    // fix numbers after dot (.) 
    // let's we want only 2 numbers
    // after dot(.)
    cout.precision(2);
    cout<<"The numbers are"<<endl;
    cout<<fixed<<a<<" "<<b<<" "<<c<<endl;
    return 0;
}
output:-
The fixed notation of the numbers are
123.458900 1000000.000000 1256.320000
The scientific notation of the numbers are
1.234589e+02 1.000000e+06 1.256320e+03
The numbers are
123.46 1000000.00 1256.32



Recommended Post:

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

Key points:-

 MCQs:-

Post a Comment

0 Comments