Hey guys,
Write a program which accepts as input a positive integer and checks, using the algorithm described below, to see whether or not the integer is divisible by 11. This particular test for divisibility by 11 was given in 1897 by Charles L. Dodgson (Lewis Carroll).
Algorithm:
As long as the number being tested has more than two digits, form a new number by:
• deleting the units digit
• subtracting the deleted digit from the shortened number
The remaining number is divisible by 11 if and only if the original number is divisible by 11.
Heres my code:
#include <iostream>
using namespace std;
int main()
{
int x;
cout << "Enter an integer number: ";
cin >> x ;
int y ;
for ( y = x ; y > 99; y = y / 10 - y % 10 )
;
cout << y << "\n" ;
if ( y % 11 == 0 )
{
cout <<y << " is evenly divisible by 11" ;
}
else
{
cout << y << " is not evenly divisible by 11 " ;
yeah, i am sorry about that, heres the code i attempted:
#include <iostream>
using namespace std;
int main()
{
int x;
cout << "Enter an integer number: ";
cin >> x;
for ( cin >>x ; x >10; x = x / 10 - x % 10 ) {
if ( x%11==0 )
cout << x << " is divisible by 11." << endl;
else
cout << x << " is not divisible by 11." << endl;
}
return 0;
but i dont think it is right though, im hoping that someone can see what i should do about it
#include <iostream>
usingnamespace std;
int main()
{
int x;
cout << "Enter an integer number: ";
cin >> x ;
// reduce the number to 2 digits:
int y ;
for ( y = x ; y > 99; y = (y / 10) - (y % 10) )
;
cout << x << " reduced to " << y << ".\n" ;
if ( y % 11 == 0 )
{
cout << "Since " << y << " is evenly divisible by 11, "
<< x << " must be too.\n" ;
}
else
{
cout << "Since " << y << " is not evenly divisible by 11, "
<< x << " must not be either.\n" ;
}
return 0;
}