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
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification 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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.