Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. void Input(int &number)
  2. {
  3. //Điều kiện nhập số nguyên dương > 0
  4. do
  5. {
  6. cout << "\nEnter number : ";
  7. cin >> number;
  8. if (number < 0)
  9. cout << "Xin moi ban nhap lai : " << endl;
  10. } while (number < 0);
  11. }
  12. void DecimalToHexadecimal(int number)
  13. {
  14. char hexadecimal[100];
  15. int i = 0,surplus;
  16. while (number > 0)
  17. {
  18. surplus = number % 16;
  19. if (surplus < 10)
  20. {
  21. //Xem bản ASCII số 0 bắt đầu từ 48
  22. surplus += 48;
  23. }
  24. else
  25. {
  26. //Xem bản ASCII chữ A bắt đầu từ 55
  27. surplus += 55;
  28. }
  29. hexadecimal[i] = surplus;
  30. number /= 16;
  31. i++;
  32. }
  33. cout << "Decimal to Hexadecimal: ";
  34. for (int j = i - 1; j >= 0; j--)
  35. {
  36. cout << hexadecimal[j];
  37. }
  38. cout << endl;
  39. }
  40. int main()
  41. {
  42. int number;
  43. Input(number);
  44. DecimalToHexadecimal(number);
  45. cout << endl;
  46. system("pause");
  47. return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement