Printing Output in Multiple Lines in C++
This article focuses on discussing how to use cout for multi-line printing. This can be easily done using any of these two methods:
- Using endl.
- Using \n.
Let’s discuss each of these methods in detail.
Using endl
The endl statement can be used to print the multi-line string in a single cout statement. Below is the C++ program to show the same:
C++
// C++ program to show endl statement // can be used to print the multi-line // string in a single cout statement #include <iostream> using namespace std; // Driver code int main() { cout << " w3wiki is best" " platform to learn " << endl << " It is used by" " students to gain knowledge" << endl << " It is really helpful" ; return 0; } |
Output
w3wiki is best platform to learn It is used by students to gain knowledge It is really helpful
Time complexity: O(1)
Auxiliary Space: O(1)
Using ‘\n’
‘\n’ can be used instead of endl to print multi-line strings. Below is the C++ program to implement the above approach:
C++
// C++ program to show '\n' can be // used instead of endl to print // multi-line strings #include <iostream> using namespace std; // Driver code int main() { cout << " w3wiki is best" " platform to learn \n It" " is used by students to" " gain knowledge \n It is" " really helpful" ; return 0; } //Main key points here are : //You can write as many statements as you want in a single line, //but I recommend you to write one statement per line to keep the code neat and clean. //Anything which is written between double quotation " " is a string literal. |
Output
w3wiki is best platform to learn It is used by students to gain knowledge It is really helpful
Time complexity: O(1)
Auxiliary Space: O(1)