Advertisement
Zeinab_Hamdy

octal2binary

Apr 7th, 2023
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define sz(x) int(x.size())
  4. #define nl "\n"
  5.  
  6.  
  7.  
  8.  
  9. void octalToBin(){
  10.  
  11.     string num ;
  12.     cout <<"Enter the number in octal to convert it to binary : " ;
  13.     cin >> num;
  14.  
  15.     vector < string > convert{"000" , "001" , "010" , "011" , "100" , "101" , "110" , "111" };
  16.  
  17.     string ans ="";
  18.     for(int i = 0; i < num.size() ; i++){
  19.         if(num[i]=='.')
  20.             ans+='.';
  21.         else if(num[i] >='0' and num[i] <='7')
  22.             ans+=convert[num[i]-'0'] ;
  23.         else
  24.             return void(cout <<"Cann't be convert this number. "<< nl);
  25.     }
  26.  
  27.     cout <<"Binary number : "<< ans << nl;
  28.  
  29. }
  30.  
  31. int main() {
  32.  
  33.  
  34.     octalToBin();
  35.  
  36.     return 0;
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement