cout hex c++

#include <iostream>
using namespace std;

int main() {
    int number = 255;

    cout << "Decimal: " << number << endl;
    cout << "Hexadecimal: " << hex << number << endl;

    return 0;
}

Explanation:

  1. #include <iostream>: Includes the input/output stream library, allowing input and output operations.
  2. using namespace std;: Allows the use of elements from the std (standard) namespace without explicitly specifying it.
  3. int main() { ... }: The main function where the program execution begins.
  4. int number = 255;: Declares an integer variable named number and assigns it the decimal value 255.
  5. cout << "Decimal: " << number << endl;: Outputs the text "Decimal: " followed by the value of the number variable in decimal format using the cout (console output) stream.
  6. cout << "Hexadecimal: " << hex << number << endl;: Outputs the text "Hexadecimal: " followed by the value of the number variable in hexadecimal format (hex) using the cout stream. The hex manipulator changes the base to hexadecimal for the subsequent output.
  7. return 0;: Indicates successful completion of the main() function and the program overall.