#include <iostream>
int main()
{
std::cout << "Enter an integer number: ";
int x = 0;
std::cin >> x;
constint base = 10;
int y;
for ( y = x; y > base; y = y / base - y % base ) { /* empty */ }
if ( y == 0 ) std::cout << x << " is divisible by 11." << std::endl;
else std::cout << x << " is not divisible by 11." << std::endl;
return 0;
}
By the way my code contains a bug. I did not take into account that x can be a negative number. So instead of the condition y > base in the loop there should be
For loops are typically used for things which we know are of a fixed limit.
While loops are used for things that could continue forever.
...To be honest, they are exactly the same, but it's how the programmer 'reads' it that is important in this case. Both can be made to do exactly the same thing. An example is that a for loop is used to cycle through an array, typically a while loop is not. Mostly because an array should have a fixed number... ....until you get to dynamic memory allocation in which case....
I think that this value 12345678901234567900 exceeds the maximum exceptable value for type int .
You should use an integer value that is not greater than INT_MAX (#include <climits>) or std::numeric_limits<int>::max (#include <limits>).
ok thanks for the suggestment, but i was wondering that someone was able to suggested a way to fix my code to delete every last digit until it divides into 11