The Wayback Machine - https://web.archive.org/web/20240118181131/https://www.geeksforgeeks.org/printing-output-in-multiple-lines-in-cpp/
Open In App
Related Articles

Printing Output in Multiple Lines in C++

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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)


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

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!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 23 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads