#include <iostream>
#include<cmath>
#include<ctime>
#include<string>
#include <iomanip>
#include <fstream>
using namespace std;
void decToBinary(int n)
{
// array to store binary number
int binaryNum[3][3];
//converting to binary
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
binaryNum[i][j] = n % 2;
n = n / 2;
}
}
// printing binary> array in reverse order
for (int i = 3-1; i >= 0; i--){
for (int j = 3 - 1; j >= 0; j--)
{
if (binaryNum[i][j] == 0)
cout << "H" << " ";
else
cout << "T" << " ";
}
cout << endl;
}
}
int main()
{
int n;
cout << "Enter a decimal number between 1 and 512 ";
cin >> n;
decToBinary(n);
return 0;
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter