MeehoweCK

Untitled

Oct 3rd, 2020
781
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5. void dec_to_bin(int liczba)
  6. {
  7.     int i=0,tab[31];
  8.  
  9.     while(liczba)
  10.     {
  11.         tab[i++]=liczba%16;
  12.         liczba/=16;
  13.     }
  14.  
  15.     for(int j=i-1; j>=0; j--)
  16.     {
  17.         if(tab[j] > 9)
  18.             cout << static_cast<char>(tab[j] + 55);
  19.         // przykładowo: "cyfra" A ma wartość 10, natomiast litera A w systemie ASCII ma numer 65. Oznacza to, że musimy dodać 55, aby zrobić konwersję
  20.         else
  21.             cout << tab[j];
  22.     }
  23. }
  24.  
  25. int main()
  26. {
  27.     int liczba;
  28.  
  29.     cout<<"Podaj liczbę: ";
  30.     cin>>liczba;
  31.  
  32.     dec_to_bin(liczba);
  33.     cout<<endl;
  34.  
  35.     system("pause");
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment