Advertisement
NabilaShova

numeric lab

Oct 31st, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. void bin(int temp)
  4. {
  5. int n,i=0,j,x,a[100];
  6. n=temp;
  7. while(n!=0)
  8. {
  9. x=n%2;
  10. n=n/2;
  11. a[i] = x;
  12. i++;
  13. }
  14. for(j=i-1; j>=0; j--)
  15. {
  16. cout << a[j];
  17. }
  18.  
  19. cout << endl;
  20. }
  21. void octal(float temp)
  22. {
  23. int n,z,i=0,j,x,a[100],b[100];
  24. n=temp;
  25. float y = temp-n;
  26. while(n!=0)
  27. {
  28. x=n%8;
  29. n=n/8;
  30. a[i] = x;
  31. i++;
  32. }
  33. cout << "Integral: ";
  34. for(j=i-1; j>=0; j--)
  35. {
  36. cout << a[j];
  37. }
  38. int k=0;
  39. while(z!=8)
  40. {
  41. z = y*8;
  42. b[k] = z;
  43. y = y-z;
  44. k++;
  45. }
  46. cout << "Fraction: ";
  47. for(j=0; j<=k; j++)
  48. {
  49. cout << b[j];
  50. }
  51. cout << endl;
  52. }
  53. void hex(int temp)
  54. {
  55. int n,i=0,j,x,a[100];
  56. n=temp;
  57. while(n!=0)
  58. {
  59. x=n%16;
  60. n=n/16;
  61. a[i] = x;
  62. i++;
  63. }
  64. for(j=i-1; j>=0; j--)
  65. {
  66. if(a[j] == 10)
  67. cout << "A";
  68. else if(a[j] == 11)
  69. cout << "B";
  70. else if(a[j] == 12)
  71. cout << "C";
  72. else if(a[j] == 13)
  73. cout << "D";
  74. else if(a[j] == 14)
  75. cout << "E";
  76. else if(a[j] == 15)
  77. cout << "F";
  78. else
  79. cout << a[j];
  80. }
  81. cout << endl;
  82. }
  83. int main()
  84. {
  85. int op;
  86. float n;
  87. cout << "Decimal to non-decimal" << endl;
  88. while(1)
  89. {
  90. cout << "Enter option: ";
  91. cin >> op;
  92. if(op == 1)
  93. {
  94. cin >> n;
  95. cout << "Binary: "<< endl;
  96. bin(n);
  97. }
  98. if(op == 2)
  99. {
  100. cin >> n;
  101. cout << "Octal: "<< endl;
  102. octal(n);
  103. }
  104. if(op == 3)
  105. {
  106. cin >> n;
  107. cout << "Hexadecimal: "<< endl;
  108. hex(n);
  109. }
  110. }
  111.  
  112. return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement