Vlad5080

Armstrong's numbers

Jan 15th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     setlocale(0, "");
  10.     int n = 0;
  11.     cout << "ЧИСЛА АРМСТРОНГА" << endl << endl;
  12.     while (true)
  13.     {
  14.         cout << "Укажите n в пределах от 1 до 10 (0 для выхода)\n -> ";
  15.         cin >> n;
  16.         if ((n > 10) || (n < 0))
  17.         {
  18.             cout << "Введены некорректные данные" << endl;
  19.         }
  20.         else if (n == 0)
  21.         {
  22.             cout << "Выход" << endl;
  23.             return 0;
  24.         }
  25.         else
  26.         {
  27.             vector<int> output;
  28.             for (int num = pow(10, n - 1); num < pow(10, n); num++)
  29.             {
  30.                 int number = num;
  31.                 int summ = 0;
  32.                 for (int i = 0; i < n; i++)
  33.                 {
  34.                     summ += pow(number % 10, n);
  35.                     number = number / 10;
  36.                 }
  37.                 if (num == summ)
  38.                 {
  39.                     output.push_back(num);
  40.                 }
  41.             }
  42.             if (output.size() != 0)
  43.             {
  44.                 cout << "Для n = " << n << " числами Армстронга являются\n -> ";
  45.                 for (int i = 0; i < output.size(); i++)
  46.                 {
  47.                     cout << output[i] << " ";
  48.                 }
  49.             }
  50.             else
  51.             {
  52.                 cout << "Для n = " << n << " нет числел Армстронга";
  53.             }
  54.             cout << endl;
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment