/*
Program for coverting "Decimal number into Binary" using Array (max 64-Bits).
*/
#include<iostream>
using namespace std;
int main()
{
int b[64], i = 0, digits = 0;
long num, temp, temp1;
//for input
cout << "Enter Number?\t";
cin >> num;
temp = num;
temp1 = num;
//for calculating bits
while (temp1 > 0)
{
temp1 /= 2;
digits++;
}
//for calculting binary
while (i <= digits)
{
b[i] = num % 2;
num /= 2;
i++;
}
cout << "Binary of " << temp << " is:\n";
for (i = digits; i >= 0; i--)
cout << b[i];
cout << endl;
return 0;
}
OUTPUT
/*
Program for coverting "Decimal number into Binary".
*/
#include <iostream>
using namespace std;
int main()
{
long long num, temp, rem, i = 1, binary = 0;
cout << "Enter the decimal to be converted?\t";
cin >> num;
temp = num;
//for calculating binary
do
{
rem = num % 2;
binary = binary + (i*rem);
num = num / 2;
i = i * 10;
} while (num>0);
cout << "Binary of " << temp << " is:\n" << binary << endl;
return 0;
}
No comments:
Post a Comment