+ 1
#iclude <iostream> using namespace std; int digits(int x) //takes a number int x { if (x<10) return 1; else return 1+digits(x/10); //returns the digits of x } int pow(int x, int exp) //x to the power of exp { if(exp==1) return x; else return x * pow(x , exp-1); //using recursion } void armstrg(int a) // cout a if armstrong { int test=a; int d=digits(a); //get a's digits int res=0; int x=a; while(x>0) { a=x%10; // rightmost digit of x res+=pow(a,d); // a to the power of d x/=10; // now next digit is rightmost } if(test==res) // so, is it an armstrong number? cout<<res<<endl; } int main() { int n; cin>>n; for(int y=0;y<=n;++y) { armstrg(n); } return 0; }
26th Sep 2016, 5:24 PM
Andres Mar