Advertisement
Guest User

Untitled

a guest
Sep 24th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int convert;
  7. int n;
  8. int binary;
  9. int num;
  10. int convertBinaryToDecimal(long long);
  11. int DecimalToBinary();
  12. int octalToDecimal(int octalNumber);
  13. int decimalToOctal(int decimalNumber);
  14. int octalNumber;
  15. int decimalNumber;
  16. long long convertDecimalToBinary(int);
  17. int binaryNumber;
  18.  
  19.  
  20. int main ()
  21. {
  22.  
  23.  
  24. cout << "Choose the conversion method: \n" <<endl;
  25. cout << "1. Binary to Decimal \n" << endl;
  26. cout << "2. Decimal to Binary \n" << endl;
  27. cout << "3. Octal to Decimal \n" << endl;
  28. cout << "4. Decimal to Octal \n" << endl;
  29.  
  30. cout << "Please enter: " << endl;
  31. cin >> convert;
  32.  
  33. if (convert==1)  // BINARY TO DECIMAL
  34. {
  35.     long long n;
  36.     cout <<"You are now converting BINARY into DECIMAL" << endl;
  37.     cout << "Please enter Binary:" << endl;
  38.     cin >> n;
  39.    
  40.     cout << n << " is " << binaryNumber << " in DECIMAL";
  41.     return 0;  
  42. } //CONVERT 1 END
  43.  
  44.  
  45. if (convert==2) // DECIMAL TO BINARY (PROOOOOOOOOOOOOOOOOOOOOOBLEM)
  46. {
  47.     cout <<"You are now converting DECIMAL into BINARY" << endl;
  48.     cout << "Please enter DECIMAL:" << endl;
  49.     cin >> n;
  50.     cout << n << " is " << convertDecimalToBinary(n) << " in BINARY";
  51.  
  52.  
  53. }
  54.  
  55. if(convert==3) // OCTAL TO DECIMAL
  56. {
  57.     cout <<"You are now converting OCTAL into DECIMAL" << endl;
  58.     cout << "Please enter OCTAL:" << endl;
  59.     cin >> octalNumber;
  60.    
  61.     cout << octalNumber << " is " << octalToDecimal(octalNumber) << " in DECIMAL";
  62.     return 0;
  63. }
  64.  
  65. if(convert==4) //DECIMAL TO OCTAL
  66. {
  67.     cout <<"You are now converting DECIMAL into OCTAL" << endl;
  68.     cout << "Please enter DECIMAL:" << endl;
  69.     cin >> decimalNumber;
  70.    
  71.     cout << decimalNumber << " is " << decimalToOctal(decimalNumber) << " in OCTAL";
  72.     return 0;
  73. }
  74. } // MAIN END
  75.  
  76.  
  77. int convertBinaryToDecimal(long long n) // FORMULA FOR CONVERTING BINARY TO DECIMAL (1)
  78. {
  79.     int decimalNumber = 0, i = 0, remainder;
  80.     while (n!=0)
  81.     {
  82.         remainder = n%10;
  83.         n /= 10;
  84.         decimalNumber += remainder*pow(2,i);
  85.         ++i;
  86.     }
  87.     return decimalNumber;
  88. }
  89.  
  90. long long convertDecimalToBinary(int n) //DECIMAL TO BINARY (2)
  91. {
  92.     long long binaryNumber = 0;
  93.     int remainder, i = 1, step = 1;
  94.  
  95.     while (n!=0)
  96.     {
  97.         remainder = n%2;
  98.         step++;
  99.         n;
  100.         remainder;
  101.         n/2;
  102.         n /= 2;
  103.         binaryNumber += remainder*i;
  104.         i *= 10;
  105.     }
  106.     return binaryNumber;
  107. }
  108.  
  109. int octalToDecimal(int octalNumber) // FORMULA FOR CONVERTING OCTAL TO DECIMAL (3)
  110. {
  111.     int decimalNumber = 0;
  112.     int i = 0;
  113.     int rem;
  114.    
  115.     while (octalNumber != 0)
  116.     {
  117.         rem = octalNumber % 10;
  118.         octalNumber /= 10;
  119.         decimalNumber += rem * pow(8, i);
  120.         ++i;
  121.     }
  122.     return decimalNumber;
  123. }
  124.  
  125. int decimalToOctal(int decimalNumber) // FORMULA FOR CONVERTING DECIMAL TO OCTAL (4)
  126. {
  127.     int rem, i = 1, octalNumber = 0;
  128.     while (decimalNumber != 0)
  129.     {
  130.         rem = decimalNumber % 8;
  131.         decimalNumber /= 8;
  132.         octalNumber += rem * i;
  133.         i *= 10;
  134.     }
  135.     return octalNumber;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement