1. could be a trick question ...
std::cout << "numbers from 1 to 100 and 100 to 1";
2. assuming we are talking integers, use a macro like ...
// macro to swap two arguments of any type
#define SWAP(x, y) ((x) ^= (y) ^= (x) ^= (y))
Edit: Eating crow again. Sorry, change the "of any type" to "integers only"!
3. assuming we are talking integers, whittle the number down with a series of odd integers (1,3,5,7,...) until a zero match is reached ...
#include <iostream>
using namespace std;
void isPowerOf2(int x)
{
int y;
y = 1; // start of odd number sequence 1, 3, 5, 7 ...
while(x > -1)
{
cout << x << endl; // test
x = x - y; // subtracting a sequence of odd numbers
y = y + 2; // next odd number
if (x == 0)
{
cout << "Bingo, we have a power of 2 number" << endl;
break;
}
else if (x < 0)
cout << "Gee, this number fails the power of 2 test" << endl;
}
}
int main()
{
isPowerOf2(98);
isPowerOf2(81);
isPowerOf2(1);
isPowerOf2(-8); // test
cin.get(); // wait
return EXIT_SUCCESS;
}
Edit: put that in here because the tone went splenetic later in this thread.
4. Multiple addition will do ...
... as long as you don't multiply by a float!