al__nasim

binary to decimal

Jul 13th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. /* C++ program to convert binary number into decimal */
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. long bin, dec = 0, rem, num, base = 1;
  7. cout << "Enter the binary number(1s and 0s) : ";
  8. cin >> num;
  9. bin = num;
  10. while (num > 0)
  11. {
  12. rem = num % 10;
  13. dec = dec + rem * base;
  14. base = base * 2;
  15. num = num / 10;
  16. }
  17. cout << "The decimal equivalent of " << bin << " : " << dec << endl;
  18. return 0;
  19. }
Advertisement
Add Comment
Please, Sign In to add comment