Open In App

How to print Colored text in C++

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

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:




// 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;
}


Output:

Program 2:




// 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;
}


Output:

Program 3:




// 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;
}


Output:



Last Updated : 01 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads