forked from AneriMehta/DivisibilityTestPrograms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestFor11.cpp
More file actions
28 lines (26 loc) · 665 Bytes
/
TestFor11.cpp
File metadata and controls
28 lines (26 loc) · 665 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
using namespace std;
int check(string str)
{
int n = str.length();
// Compute sum of even and odd digit
int oddDigSum = 0, evenDigSum = 0;
for (int i=0; i<n; i++)
{
// When i is even, position of digit is odd
if (i%2 == 0)
oddDigSum += (str[i]-'0');
else
evenDigSum += (str[i]-'0');
}
// Check its difference is divisble by 11 or not
return ((oddDigSum - evenDigSum) % 11 == 0);
}
int main()
{
string str;
cout << "Enter number: " ;
cin >> str ;
check(str)? cout << "Number divisible by 11" : cout << "Number not divisible by 11";
return 0;
}