+ 1
write a program converting decimal -binary system
4 Antworten
+ 2
thanks mohammed ...
I found a solution..
#include <iostream>
using namespace std;
int main()
{
long m,n,x,s=0,p=1;
cout<<"enter decimal number "<<endl;
cin>>n;
m=n;
while (n>0)
{
x=n%2;
n=n/2;
s=s+(x*p);
p=p*10;
}
cout<<s<<endl;
return 0;
}
I hope this is a perfect program...;)
0
#include <iostream>
using namespace std;
int main()
{
int m,n,x;
cout<<"enter decimal number "<<endl;
cin>>n;
m=n;
while (n>0)
{
x=n%2;
n=n/2;
cout<<x;
}
return 0;
}
0
Utkarsh your ideais good but it will give reverse answer for example it will show output 0011 for 12. However binary equivalent for 12 is 1100.
How about this one?
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int dec, bin, orig_dec;
string result="", inc;
cout << "Enter any decimal integer: ";
cin >> dec; cout << endl;
orig_dec = dec;
cout << "The binary equivalent of " << orig_dec << " is ";
if(dec!=0)
cout << "1";
else
cout << "0";
while(dec>1)
{
bin = dec%2;
dec = dec / 2;
if(bin == 1)
inc = "1";
else
inc = "0";
result = inc + result;
}
cout << result << endl;
return 0;
}
0
will anyone explain me how it is working