Advertisement
Guest User

Project Euler #16: Power digit sum

a guest
Dec 6th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     int tests, squared, sumofDigits;
  11.     int digit = 0;
  12.     int base = 2;
  13.     int array[3];
  14.  
  15.     cin >> tests;
  16.  
  17.     if (1 <= tests && tests <= 100)
  18.     {
  19.         for (int i = 0; i < tests; i++)
  20.         {  
  21.             cin >> digit;
  22.             if (1 <= digit && digit <= 10000)
  23.             {
  24.                 int square = pow(base,digit);
  25.                 squared = square;
  26.  
  27.                 for (int i = 2; i >= 0; i--)
  28.                 {
  29.                     array[i] = squared % 10;
  30.                     squared /= 10;
  31.                 }
  32.            
  33.                 sumofDigits = array[0] + array[1] + array[2];
  34.                 cout << sumofDigits << endl;  
  35.             }
  36.         }
  37.     }
  38.         return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement