Advertisement
MeehoweCK

Untitled

May 25th, 2023
1,023
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. string odwroc_wyraz(string napis)
  6. {
  7.     string wynik = "";
  8.  
  9.     for (int i = napis.size() - 1; i >= 0; --i)
  10.         wynik += napis[i];
  11.  
  12.     return wynik;
  13. }
  14.  
  15. int main()
  16. {
  17.     int liczba;
  18.     cout << "Podaj liczbe calkowita:" << endl;
  19.     cin >> liczba;
  20.  
  21.     string liczbadwojkowa = "";
  22.  
  23.     if (liczba == 0)
  24.         liczbadwojkowa = "0";
  25.     else
  26.     {
  27.         int temp = liczba;
  28.         while (temp > 0)
  29.         {
  30.             if (temp % 2 == 0)
  31.             {
  32.                 liczbadwojkowa += '0';  // cout << "0";
  33.             }
  34.             else
  35.                 liczbadwojkowa += '1';  // cout << "1";
  36.             temp /= 2;
  37.         }
  38.  
  39.         liczbadwojkowa = odwroc_wyraz(liczbadwojkowa);
  40.     }
  41.  
  42.     cout << liczba << " w systemie binarnym to " << liczbadwojkowa << endl;
  43.  
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement