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.
Next Steps:
- Get Free Certificate of Merit in C++ Programming
- Participate in C++ Programming Certification Contest
- Become a Top Ranker in C++ Programming
- Take C++ Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Buy C++ Books
- Apply for Computer Science Internship
- Buy Computer Science Books
- Apply for Information Technology Internship
- Practice Programming MCQs