Open In App
Related Articles

Printing Output in Multiple Lines in C++

Improve Article
Improve
Save Article
Save
Like Article
Like

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 <<" GeeksforGeeks is best"
           " platform to learn " << endl << " It is used by"
           " students to gain knowledge" <<
           endl << " It is really helpful";
    return 0;
}


Output

 GeeksforGeeks 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 << " GeeksforGeeks 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

 GeeksforGeeks 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)


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 23 Jul, 2022
Like Article
Save Article
Previous
Next
Similar Reads