C++ Program to demonstrate use of Binary, Octal and Hexadecimal numbers as Integer literals [DEVCPP/GCC]


In C++, we can use Binary numbers, Octal numbers and Hexadecimal numbers as Integer literal.

For declaring an Binary Integer literal, the Binary digits are placed after 0b or 0B.
eg. <0b>101 i.e.  Binary (101)= Decimal (5)

For declaring an Octal Integer literal, the Octal digits are placed after 0.
eg. <0>112 i.e.  Octal(112)= Decimal (156)

For declaring a Hexadecimal Integer literal, the Hexadecimal digits are placed after 0x or 0X.
eg. <0x>12 i.e.  Hexadecimal(12)= Decimal (18)


//Program to demonstrate use of Binary, Octal and Hexadecimal numbers as Integer literals 

#include <iostream>

using namespace std;

int main()
{
int a = 0106;                  // Octal(106)= Decimal(70)
cout<< a << endl;

int b = 0x46;                 // Hexadecimal(46)=Decimal(70)
cout<< b << endl;

int c = 0B1000110;      // Binary(1000110) = Decimal(70)
cout<< c;

return 0;
}


Share this

Related Posts

FIND US ON FACEBOOK!