
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print leading zeros with C++ output operator
Here we will see how to print leading zeros as output in C++. We know that if we directly put some zeros before some numeric values, then all zeros are discarded, and only exact numbers are printed.
Printing Leading Zeros with C++ Output Operator
We can manipulate the output sequence in C++ by utilizing the iomanip header. In this header, we have two manipulators, setw() and setfill(). The setw() function is used to create space between the previous text and the current text, and then we use setfill(char) to add characters to that field.
Example of Printing Leading Zeros
In this example, we will see how we can print leading zeros with a C++ Output Operator:
#include<iostream> #include<iomanip> using namespace std; int main() { int number = 256; // set 5 times leading 0 to 256 cout << setw(8) << setfill('0') << number; }
Following is the output of the code:
00000256
Advertisements