Advertisement
BojidarDosev

zad1 convert system

Nov 23rd, 2023
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5.  
  6.  
  7. int main()
  8. {
  9.     // Declare and read the value of x
  10.     int x;
  11.     cin >> x;
  12.     // Declare and read the value of y
  13.     int y;
  14.     cin >> y;
  15.     // Declare variable result and save the converted value of y into x number system in it
  16.     string result;
  17.     if (x < 2 || x>16 || y < 1 || y>100)
  18.     {
  19.         cout << "Invalid input data!";
  20.         return 1;
  21.     }
  22.     while (y > 0)
  23.     {
  24.         int ostatuk = y % x;
  25.         char sym;
  26.         if (ostatuk < 10)
  27.         {
  28.             sym = '0' + ostatuk;
  29.         }
  30.         else
  31.         {
  32.             sym = 'A' + ostatuk - 10;
  33.         }
  34.  
  35.         result = sym + result;
  36.         y /= x;
  37.     }
  38.     // Print result to the console
  39.     cout << result;
  40.     return 0;
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement