15,850,298 members
1.00/5 (1 vote)
See more:
Given a number n find out how many fragments of the given number is divisible by 11 for example:

Input:

1215598
Output:

4
The numbers 55,121,12155,15598 are continuous fragments of the number which are divisible by 11.

I want to write a program in C++ to do the same but I am unable to figure out an efficient and suitable way.

What I have tried:

```#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int DivisibilityByEleven(int num);
int num;
int main()
{
int result=DivisibilityByEleven(num);
return 0;
}

int DivisibilityByEleven(int num)
{
int counter=0;
cin>>num;
vector<int> arr;
while(num!=0)
{
int temp=num%10;
num=num/10;
arr.push_back(temp);
}
reverse(arr.begin(),arr.end());
for(int i=0;i<arr.size();i++)
{
cout<<arr[i];
}

if(num%11==0)
{
counter++;
}
}```
Posted
Updated 22-Apr-21 10:12am
CHill60 12-Sep-18 7:57am
What is wrong with the code you already have?
Member 13970073 12-Sep-18 8:18am
it is just taking the integer value into an array
I need to break it up in fragments

## Solution 2

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Think about the task: how many "contiguous fragments" of the input number are there?
The minimum size of a fragment is 2 (or it's not divisible by 11) and the maximum is the whole number length of 7 (or if that is not allowed then one less - 6)
Which means that all the contiguous fragments are:
C#
```12, 21, 15, 55, 59, 98,
121, 215, 155, 559, 598,
1215, 2155, 1559, 5598,
12155, 21559, 15598,
121559, 215598,
1215598```
They are easy to work out with a pair of nested loops.
Then all you have to do is check each fragment for divisibility, and that's trivial: the modulus operator "%" will do that for you!
Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!

v2
jsc42 12-Sep-18 8:40am
Minor Typo: in the list of 2 digit combinations, the values should be
12, 21, 15, 55, 59, 98
OriginalGriff 12-Sep-18 8:57am
Fixed! (My fingers were clearly off to the right again...)

## Solution 1

I would use the string representation of the number:
C++
```#include <iostream>
#include <sstream>

using namespace std;

// get the integer corresponding to the substring starting at 'pos', having length 'len'
int slice_to_int(const string & str, size_t pos, size_t len)
{
istringstream iss(str.substr(pos, len));
int n;
iss >> n;
return n;
}

int main()
{
int n = 1215598;

ostringstream oss;
oss << n;

string s = oss.str(); // get the string representation of the number

size_t len = s.length();
size_t count = 0;

for (size_t i = 0; i<len-1; ++i)
{
for (size_t j = 2; j <= (len - i) ; ++j)
{
int k = slice_to_int( s, i, j);
if ( (k % 11) == 0)
{
++count;
}
}
}

cout << "count = " << count << endl;
}```

## Solution 3

```#include<stdio.h>

int DivisibilityByELeven(int num)
{
int d=0;
int temp;
for(int i=num;i>0;i=i/10)
{
d++; //Counting the number of digits in the parameter/
}
printf("%d\n",d);
if(d==2)
{
if(num%11==0)
{
return 1;
}
}
else
{

int c=0; //count variable
int div=100;
temp=num;
int count=0;
while(temp>0)
{
while(count<d)
{

int r= temp%div;
if(r%11==0)
{
printf("%d\n",r);
c++;
}
count++;
div *=10;
}
d--;
temp=temp/10;
count=0;
div=10;
}
return c;
}
}
int main()
{
int num;
scanf("%d",&num);
int res= DivisibilityByELeven(num);
printf("%d",res);
}```

v2
CHill60 22-Apr-21 17:18pm
An uncommented, unexplained code dump is not a good solution. Giving someone a ready-baked answer for their homework doesn't help them really. Doing all that over 2 years after they needed the answer...priceless

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Richard MacCutchan 50 k5054 50 Dave Kreskowiak 25 Member 16214273 10 Sandeep Mewara 10
 Richard MacCutchan 70 k5054 60 Dave Kreskowiak 43 OriginalGriff 40 Richard Deeming 40

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900