Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: bellazio on May 2nd, 2012  |  syntax: C++  |  size: 0.34 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include<iostream>
  2. #include<cmath>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     int i,r=0;
  9.     string n;
  10.  
  11.     cout<<"inserisci un numero binario\n";
  12.     cin>>n;
  13.  
  14.     for(i=0;i<=n.length()-1;i++)
  15.     {
  16.         if(n.substr(i,1)=="1")
  17.         {
  18.             r=r+pow(2,n.length()-1-i);
  19.         }
  20.     }
  21.     cout<<r<<endl;
  22.     return 0;
  23. }