-3

This one is hard because i can't find it in the internet. Please write a program where you would not use the math.h library in C++ where in you would display all the armstrong numbers from 1 to the range inputted by the user.

3
  • 11
    The internet is not there to do your homework. Please make an attempt first, and then show the code you've got and describe what you're stuck with; then you might get help. Also, please add the "homework" tag to your question. Commented Sep 4, 2012 at 8:27
  • 1
    The first hit in google also gives a pascal code how to do it. You are actually asking for someone to completely make the HW for you?
    – amit
    Commented Sep 4, 2012 at 8:28
  • since you are dealing with armstrong numbers you only have to deal with integer powers and this is quite easy. if you are on a course that deals with armstrong numbers writing a power function valid only for integers should not be beyond you Commented Sep 4, 2012 at 8:33

1 Answer 1

2

Won't give you full code - use subsequent multiplications to find the power. The rest should be straight-forward.

Here's some code that, if this is homework, you can't use since it'll get you in trouble:

bool isArmstrongNumber(int x)
{
   //some magic happens here
}

template<int x>
struct armstrong_number
{
    armstrong_number()
    {
        if ( isArmstrongNumber(x) )
            std::cout << x << endl;
        armstrong_number<x-1> y;
    }
};

template<>
struct armstrong_number<0>
{
    armstrong_number()
    {
        std::cout << 0 << endl;
    }
};

int main()
{
    armstrong_number<999> x;
}
1
  • 2
    +1 for giving "unsubmittable" code :D Commented Sep 4, 2012 at 8:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.