Advertisement
skb50bd

Convert Decimal to (Binary, Octal, Hexadecimal)

Mar 29th, 2016
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.43 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int binary(int dec) {
  5.     int bin = 0;
  6.     int rem;
  7.     for(int i = 1; dec; i *= 10) {
  8.         rem = dec % 2;
  9.         dec /= 2;
  10.         bin += rem * i;
  11.     }
  12.     return bin;
  13. }
  14.  
  15. int main() {
  16.     int n;
  17.  
  18.     cin >> n;
  19.  
  20.     cout << "Binary: " << binary(n) << endl;
  21.     cout << "Octal: " << oct << n << endl;
  22.     cout << "Hexa-decimal: " << hex << n << endl;
  23.  
  24.     return 0;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement