Let n = akak-1ak-2. . .a1a0 be an integer and t = a0 - a1 + a2 - + (-1)k ak. It
is known that n is divisible by 11 if and only if t is divisible by 11. For
example, suppose that n = 8784204. Then, t = 4 - 0 + 2 - 4 + 8 - 7 + 8 =
11. Because 11 is divisible by 11, it follows that 8784204 is divisible by 11.
If n ¼ 54063297, then t = 7 - 9 + 2 - 3 + 6 - 0 + 4 - 5 = 2. Because 2 is not divisible by 11, 54063297 is not divisible by 11.Write a program that
prompts the user to enter a positive integer and then uses this criterion to
determine whether the number is divisible by 11.
1
Expert's answer
2012-12-25T10:09:34-0500
#include <iostream> #include <conio.h>
using namespace std;
void main() { long long number; cout << "Enter a positive integer: "; cin >> number;
char digit; char sign = 1; int sum = 0;
while (number > 0) { digit = number % 10; number /= 10;
sum += sign * digit; sign *= -1; }
if (sum % 11 == 0) cout << number << " is divisible by 11." << endl; else cout << number << " is not divisible by 11." << endl; getch(); }
Comments
Leave a comment