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.
/*
* C++ Program to Demonstrate Use of Formatting Flags on Integer Output
*/
#include <iostream>
using namespace std;
int main()
{
int data;
ios_base::fmtflags old;
cout << "Enter a number : ";
cin >> data;
old = cout.flags();
cout.setf(ios_base::oct, ios_base::basefield);
cout << "\nOctal format : " << data << endl;
cout.setf(ios_base::hex, ios_base::basefield);
cout << "Hexa-dec format : " << data << endl;
cout.setf(old, ios_base::basefield);
cout << "Default format : " << data << endl;
}
$ 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]Related Posts:
- Check C++ Books
- Check Computer Science Books
- Practice Programming MCQs
- Practice Computer Science MCQs
- Apply for C++ Internship