The Code given below can be used in TURBO C++ Compilers: -
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int binary[100], num, i=0, j;
cout<<"Enter a decimal number: ";
cin>>num;
while (num>0)
{
binary[i]=num%2;
num=num/2;
i++;
}
cout<<"The binary equivalent is ";
for (j=i-1; j>=0; j--)
cout<<binary[j];
getch();
}
The Code given below can be used in gcc/g++ Compilers: -
#include <iostream>
using namespace std;
int main()
{
int num, i=0, j=0, x;
cout<<"Enter a decimal number: ";
cin>>num;
x=num;
while (x>0)
{
x=x/2;
j++;
}
int binary[j];
while (num>0)
{
binary[i]=num%2;
num=num/2;
i++;
}
cout<<"The binary equivalent is ";
for (j=i-1; j>=0; j--)
cout<<binary[j];
return 0;
}
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP