Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // int size adalah ukuran biner, oktal dan hexa
  5. // int n adalah bilangan
  6. void convert_from_decimal(int SIZE, int n) {
  7.  
  8. int a[10], i;
  9.  
  10. // membagi
  11. for(i=0; n>0; i++) {
  12.  
  13. //mencari sisa hasil bagi
  14. a[i] = n % SIZE;
  15.  
  16. // membagi
  17. n = n / SIZE;
  18. }
  19.  
  20. // membalik array untuk ditampilkan
  21. for(i=i-1; i >= 0; i--) {
  22.  
  23. // biner dan desimal hanya print bilangan
  24. if (a[i] < 10) {
  25. cout << a[i];
  26.  
  27. // dari A-F hanya hexa
  28. } else if (a[i] == 10) {
  29. cout << 'A';
  30. } else if (a[i] == 11) {
  31. cout << 'B';
  32. } else if (a[i] == 12) {
  33. cout << 'C';
  34. } else if (a[i] == 13) {
  35. cout << 'D';
  36. } else if (a[i] == 14) {
  37. cout << 'E';
  38. } else if (a[i] == 15) {
  39. cout << 'F';
  40. }
  41. }
  42.  
  43. cout << endl;
  44. }
  45.  
  46. int main() {
  47.  
  48. int n;
  49. cout << "Desimal: ";
  50. cin >> n;
  51.  
  52. cout << "Biner: "; convert_from_decimal(2, n);
  53. cout << "Oktal: "; convert_from_decimal(8, n);
  54. cout << "Hexa: "; convert_from_decimal(16, n);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement