How to print Colored text in C++

In C++ programming, the background of the output screen is black and text color is in white color. We can color both the background and text color in the output screen in the following ways.

Header File:

#include <windows.h>  
   or
#include <stdlib.h>

Syntax:

system("Color XY")

In the above command to change the background color change the value X of the above syntax to the corresponding color you want and to change the text color change the value Y of the above syntax to the corresponding color you want.

Below is the table for allow color in C++:



Color id Color Color id Color
1 Blue 9 Light Blue
2 Green 0 Black
3 Aqua A Light Green
4 Red B Light Aqua
5 Purple C Light Red
6 Yellow D Light Purple
7 White E Light Yellow
8 Gray F Bright White

Below is the illustration of Text coloring in C++:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate coloring
#include <iostream>
#include <windows.h>
using namespace std;
  
// Driver Code
int main()
{
    // 0 for background Color(Black)
    // A for text color(Green)
    system("Color 0A");
  
    // Print any message
    cout << "Geeks For Geeks!";
    return 0;
}

chevron_right


Output:

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate coloring
#include <iostream>
#include <stdlib.h>
using namespace std;
  
// Driver Code
int main()
{
    // Print any message
    cout << "Geeks For Geeks!";
  
    // We can print the statement first
    // and then changed the color
  
    // E for background Color(Light Yellow)
    // 4 for text color(Red)
    system("Color E4");
  
    return 0;
}

chevron_right


Output:

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate coloring
#include <iostream>
#include <stdlib.h>
using namespace std;
  
// Driver Code
int main()
{
    // B for background Color(Light Aqua)
    // 5 for text color(Purple)
    system("Color B5");
    cout << "Geeks";
  
    // 1 for background Color(Blue)
    // 6 for text color(Yellow)
    system("Color 16");
    cout << " For ";
  
    // D for background Color(Light Purple)
    // E for text color(Light Yellow)
    system("Color DE");
    cout << "Geeks";
  
    return 0;
}

chevron_right


Output:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.