Advertisement
Luxeon94

17/18 Q2: Conversion from binary number to octal; X72799

Nov 15th, 2018
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int binary2octal(int n) {
  5.     // your code here
  6.     if(n/1000==0){
  7.         if(n%1000==0) return 0;
  8.         else if(n%1000==1) return 1;
  9.         else if(n%1000==10) return 2;
  10.         else if(n%1000==11) return 3;
  11.         else if(n%1000==100) return 4;
  12.         else if(n%1000==101) return 5;
  13.         else if(n%1000==110) return 6;
  14.         else return 7;
  15.     }
  16.     else return binary2octal(n/1000)*10+binary2octal(n%1000);
  17. }
  18.  
  19. int main(){
  20.     int n;
  21.     while (cin >> n){
  22.         cout  << n << " is equivalent to " << binary2octal(n);
  23.         cout  << " in base 8" << endl;
  24.     }    
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement