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