avr39ripe

cppChange4DigitNum

Sep 25th, 2020 (edited)
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5.     const int max3digit{ 999 };
  6.     const int min5digit{ 10000 };
  7.  
  8.     // enum Pos is like four constants packed together under one roof
  9.     // It simply replaces
  10.     // const int one {1};
  11.     // const int two {10};
  12.     // const int three {100};
  13.     // const int four {1000};
  14.  
  15.     enum Pos { one = 1, two = 10, three = 100, four = 1000 };
  16.  
  17.     int num{ 1234 };
  18.     int res{ 0 };
  19.  
  20.     std::cout << "Enter four digit number\n";
  21.     std::cin >> num;
  22.  
  23.     if (num > max3digit and num < min5digit)
  24.     {
  25.         res += (num % 10) * Pos::two;
  26.         num /= 10;
  27.         res += (num % 10) * Pos::one;
  28.         num /= 10;
  29.         res += (num % 10) * Pos::four;
  30.         num /= 10;
  31.         res += (num % 10) * Pos::three;
  32.  
  33.         std::cout << "Changed digits: " << res << '\n';
  34.     }
  35.     else
  36.     {
  37.         std::cout << "Not a four digit number!\n";
  38.     }
  39.  
  40.     return 0;
  41. }
  42.  
Add Comment
Please, Sign In to add comment