Pages

Friday, October 20, 2017

Formatted output in C++


Formatted output in C++
#include <iostream>
using namespace std;
int main()
{
  cout.precision(5) ;
  cout.width(10);
  cout << 12.345678 << "\n"; // output 12.346

  cout.fill('-');
  cout.width(10);
  cout << 12.345678 << "\n"; // output ----12.346

cout.width(10);
  cout << "Hello!" << "\n"; // Output ----Hello!
  cout.width(10);

cout.setf(ios::left); // left justify
  cout << 12.345678; // Output 12.346----
  return 0;
}

Output of program

    12.346
----12.346
----Hello!
12.346----

No comments:

Post a Comment