Advertisement
HakdogForce

Untitled

Sep 24th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. // BINARY - DECI VICE VERSA (di pa tapos)
  6. int dec_bin(int x) // decimal to binary start
  7. {
  8. int incr, res;
  9. incr = 1;
  10. res = 0;
  11.  
  12. while(x != 0)
  13. {
  14. if (x % 10 == 1){
  15. res = res + incr;
  16. x = x-1;
  17. }
  18. x = x / 10;
  19. incr = incr*2;
  20. }
  21. cout << res;
  22. }
  23.  
  24. void bin_dec(int x) // binary to decimal start
  25. {
  26. if (x / 2 != 0) {
  27. bin_dec(x / 2);
  28. }
  29. cout << x % 2;
  30. }
  31. void
  32.  
  33. int main()
  34. {
  35. int order;
  36. int x;
  37. cout << "CHOOSE PROCESS\n********************\n1. DECIMAL TO BINARY\n2. BINARY TO DECIMAL\n********************" << endl;
  38. cout << "= ";
  39. cin >> order;
  40. if (order == 1){
  41. system("cls");
  42. cout << "Converting - DECIMAL TO BINARY" << endl;
  43. cout << "Enter a number: ";
  44. cin >> x;
  45. bin_dec(x);
  46. }
  47. else if (order == 2){
  48. bin_dec(x);
  49. system("cls");
  50. cout << "Converting - BINARY TO DECIMAL" << endl;
  51. cout << "Enter a number: ";
  52. cin >> x;
  53. dec_bin(x);
  54. }
  55. else{
  56. cout<<"Null - B O B O!";
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement