Skip to content

Instantly share code, notes, and snippets.

@paintedpotato
Created May 9, 2024 11:23
Show Gist options
  • Save paintedpotato/e86487bc5bc022577684cc31d351940f to your computer and use it in GitHub Desktop.
Save paintedpotato/e86487bc5bc022577684cc31d351940f to your computer and use it in GitHub Desktop.
Hydrogen to Calcium in Binary
// Online C++ compiler to run C++ program online
#include <iostream>
using namespace std;
int decToBinary(int n)
{
// array to store binary number
int binaryNum[32];
// counter for binary array
int i = 0;
while (n > 0) {
// storing remainder in binary array
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
// printing binary array in reverse order
int binaryValue = 0;
for (int j = i - 1; j >= 0; j--)
binaryValue = binaryValue * 10 + binaryNum[j];
return binaryValue;
}
int main() {
int n = 0;
for(int i=1;i<=20;i++)
{
cout<<decToBinary(i);
if(i==1)
cout<<decToBinary(i);
else if(i==4||i==8||i==17||i==19)
cout<<decToBinary(i*2)+1;
else if(i==18)
cout<<decToBinary((i+1)*2)+1;
else cout<<decToBinary(i*2);
if(i!=20) cout<<"-";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment