C++ Program to Demonstrate Use of Formatting Flags on Integer Output

This C++ program demonstrates the use of formatting flags on integer output. The formatting flags provided in the header ios_base helps to format the integer output. The integer output can be either in decimal, octal or hexadecimal format.

Here is the source code of the C++ program which demonstrates the use of formatting flags on integer output. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Demonstrate Use of Formatting Flags on Integer Output
  3.  */
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     int data;
  10.     ios_base::fmtflags old;
  11.  
  12.     cout << "Enter a number  : ";
  13.     cin >> data;
  14.     old = cout.flags();
  15.     cout.setf(ios_base::oct, ios_base::basefield);
  16.     cout << "\nOctal format    : " << data << endl;
  17.     cout.setf(ios_base::hex, ios_base::basefield);
  18.     cout << "Hexa-dec format : " << data << endl;
  19.     cout.setf(old, ios_base::basefield);
  20.     cout << "Default format  : " << data << endl;
  21. }

$ a.out
Enter a number  : 32
 
Octal format    : 40
Hexa-dec format : 20
Default format  : 32

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.