write a program converting decimal -binary system | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

write a program converting decimal -binary system

19th Sep 2016, 4:40 PM
Meena
Meena - avatar
4 Answers
+ 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...;)
20th Sep 2016, 2:58 AM
Descifrador
Descifrador - avatar
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; }
19th Sep 2016, 4:49 PM
Descifrador
Descifrador - avatar
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; }
19th Sep 2016, 9:02 PM
Mohammed Maaz
Mohammed Maaz - avatar
0
will anyone explain me how it is working
25th Sep 2016, 1:56 PM
Saptarshi Saha
Saptarshi Saha - avatar