Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void Input(int &number)
  5. {
  6. do
  7. {
  8. cout << "\nEnter number: ";
  9. cin >> number;
  10. if (number < 0)
  11. {
  12. cout << "Please enter again" << endl;
  13. }
  14. } while (number < 0);
  15. }
  16. void DecimalToBinary(int number)
  17. {
  18. int binary[32];
  19. int i = 0;
  20. while (number > 0)
  21. {
  22. binary[i] = number % 2;
  23. number /= 2;
  24. i++;
  25. }
  26. cout << "Decimal to Binary: ";
  27. for (int j = i - 1; j >= 0; j--)
  28. {
  29. cout << binary[j];
  30. }
  31. cout << endl;
  32. }
  33. void DecimalToHexadecimal(int number)
  34. {
  35. char hexadecimal[100];
  36. int i = 0, surplus;
  37. while (number > 0)
  38. {
  39. surplus = number % 16;
  40. if (surplus < 10)
  41. {
  42. surplus += 48;
  43. }
  44. else
  45. {
  46. surplus += 55;
  47. }
  48. hexadecimal[i] = surplus;
  49. number /= 16;
  50. i++;
  51. }
  52. cout << "Decimal to Hexadecimal: ";
  53. for (int j = i - 1; j >= 0; j--)
  54. {
  55. cout << hexadecimal[j];
  56. }
  57. cout << endl;
  58. }
  59. int BinaryToDecimal()
  60. {
  61. char Bin[20] = { 0 };
  62. long int Dec = 0;
  63. int i = 0, n;
  64. cout << "\nEnter Binary number :";
  65. cin >> Bin;
  66. while (Bin[i] != '\0')
  67. {
  68. n = Bin[i] - 48;
  69. if ((n != 0) && (n != 1))
  70. return -1;
  71. Dec = (Dec * 2) + n;
  72. i++;
  73. }
  74. return Dec;
  75. }
  76. void BinaryToHexadecimal(int number)
  77. {
  78. unsigned int hexa[32] = { 0 };
  79. int i = 1, j, rem, dec = 0;;
  80. //binary to decimal
  81. while (number > 0)
  82. {
  83. rem = number % 2;
  84. dec = dec + (rem * i);
  85. number /= 10;
  86. i *= 2;
  87. }
  88. //decimal to hexa
  89. i = 0;
  90. while (dec != 0)
  91. {
  92. hexa[i] = dec % 16;
  93. dec /= 16;
  94. i++;
  95. }
  96. cout << "Hexa Decimal valua: ";
  97. for (j = i - 1; j >= 0; j--)
  98. {
  99. if (hexa[j] > 9)
  100. cout << char(hexa[j] + 55);
  101. else
  102. cout << hexa[j];
  103. }
  104. cout << endl;
  105. system("pause");
  106. return;
  107. }
  108. void HexadecimalToDecimal()
  109. {
  110. char Hex[17];
  111. long int Decimal = 0;
  112. int i = 0, val, len;
  113. cout << "Enter any Hexadecimal number: ";
  114. cin >> Hex;
  115. len = strlen(Hex);
  116. len--;
  117. for (i = 0; Hex[i] != '\0'; i++)
  118. {
  119. switch (Hex[i])
  120. {
  121. case 0:
  122. val = 0;
  123. break;
  124. case 1:
  125. val = 1;
  126. break;
  127. case 2:
  128. val = 2; break;
  129. case 3:
  130. val = 3; break;
  131. case 4:
  132. val = 4; break;
  133. case 5:
  134. val = 5; break;
  135. case 6:
  136. val = 6; break;
  137. case 7:
  138. val = 7; break;
  139. case 8:
  140. val = 8; break;
  141. case 9:
  142. val = 9; break;
  143. case 'a':
  144. case'A':
  145. val = 10; break;
  146. case 'b':
  147. case'B':
  148. val = 11; break;
  149. case'C':
  150. case'c':
  151. val = 12; break;
  152. case'd':
  153. case'D':
  154. val = 13; break;
  155. case 'E':
  156. case'e':
  157. val = 14; break;
  158. case 'F':
  159. case'f':
  160. val = 15; break;
  161. default:
  162. break;
  163. }
  164. Decimal += val * (pow(16, len));
  165. len--;
  166. }
  167. cout << "Hexadecimal to Decimal : " << Decimal << endl;
  168. }
  169. int main()
  170. {
  171. int number;
  172. int selection;
  173. do
  174. {
  175. cout << "1: Decimal to Binary " << endl;
  176. cout << "2: Decimal to Hexadecimal " << endl;
  177. cout << "3: Binary to Decimal " << endl;
  178. cout << "4: Hexadecimal to Decimal " << endl;
  179. cout << "5: Binary to Hexadecimal " << endl;
  180. cout << "6: Close(0)" << endl;
  181. cout << "\nChoose :";
  182. cin >> selection;
  183. switch (selection)
  184. {
  185. case 1:
  186. Input(number);
  187. DecimalToBinary(number);
  188. break;
  189. case 2:
  190. Input(number);
  191. DecimalToHexadecimal(number);
  192. break;
  193. case 3:
  194. cout << "Binary to Deciaml : " << BinaryToDecimal() << endl;
  195. break;
  196. case 4:
  197. HexadecimalToDecimal();
  198. break;
  199. case 5:
  200. Input(number);
  201. BinaryToHexadecimal(number);
  202. break;
  203. default:
  204. break;
  205. }
  206. } while (selection > 0);
  207. cout << endl;
  208. system("pause");
  209. return 0;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement