# C++ Do/While loop divisible by 11

Page 1 of 1

## 5 Replies - 4904 Views - Last Post: 04 October 2012 - 03:22 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294290&amp;s=23278a212dd1dd1550f72a13edd9b2cb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 khull1989

Reputation: 0
• Posts: 23
• Joined: 19-May 09

# C++ Do/While loop divisible by 11

Posted 04 October 2012 - 01:19 PM

Hey guys,

I have an issue with this program and I'm almost positive that I am going about it all wrong.

The idea of the program is to take an integer (well call this N = Ak,Ak-1,Ak-2...A0), and take the sum of the numbers (well call this S = A0 - A1 + A2 - .... +(-1)k Ak), and as it is known that N is divisible by 11 if and only if S is divisible by 11. I have the structure correct (at least I hope so), but I have that strange feeling I'm going about this all wrong.

```// include statement(s).
#include <iostream>

// using namespace statement.

using namespace std;

// Declare named constants, if necessary.

int main()
{
// Declare named variables, if necessary.

int PosInt, temp, sum;

// Executable statement(s).

cout << "Please Enter a Positive Integer ";
cin >> PosInt;
cout << endl;

temp = PosInt;
sum = 0;
do
{
sum = sum + PosInt % 10;

PosInt = PosInt / 11;
}
while (PosInt > 0);

cout << "The sum of the digits = " << sum << endl;

if (sum % 11 == 0)
cout << temp << " is divisible by 11" << endl;

else
cout << temp << " is not divisible by 11" << endl;

return 0;
}

```

Any help/guidance in the right direction is greatly appreciated!

Is This A Good Question/Topic? 0

## Replies To: C++ Do/While loop divisible by 11

### #2 jimblumberg

Reputation: 4887
• Posts: 15,264
• Joined: 25-December 09

## Re: C++ Do/While loop divisible by 11

Posted 04 October 2012 - 01:54 PM

Since you don't get the proper output I would say you are something wrong. For example if I enter 11, which is divisible by 11 but this is the output I got:
```Please Enter a Positive Integer 11

The sum of the digits = 2
11 is not divisible by 11

```

Where are you summing the numbers for the above example the numbers should be: 1+2+3+4+5+6+7+8+9+10+11 = 66. 66 happens to be divisible by 11.

Also you need to work on your indentation. Consistent indentation will help make your program easier to read and maintain.

Jim

This post has been edited by jimblumberg: 04 October 2012 - 01:55 PM

### #3 mojo666

Reputation: 397
• Posts: 856
• Joined: 27-June 09

## Re: C++ Do/While loop divisible by 11

Posted 04 October 2012 - 02:04 PM

```PosInt = PosInt / 11;  //should be PosInt = PosInt / 10;
```

Also, you are forgetting to alternate the sums. Every other number should be subtracted, not added.

### #4 khull1989

Reputation: 0
• Posts: 23
• Joined: 19-May 09

## Re: C++ Do/While loop divisible by 11

Posted 04 October 2012 - 03:07 PM

I know that I have to multiply by -1, but I'm not exactly sure where I would put that. I think that is where my problem lies with getting the wrong sum of the numbers.

### #5 mojo666

Reputation: 397
• Posts: 856
• Joined: 27-June 09

## Re: C++ Do/While loop divisible by 11

Posted 04 October 2012 - 03:16 PM

You need to multiply your Ak times -1 every other loop. You can multiple Ak times 1 in the remaining loops. As a hint, consider the following.

int x= -1
1*x=-1
-1*x=1
1*x=-1
-1*x=1
...and so on.

### #6 khull1989

Reputation: 0
• Posts: 23
• Joined: 19-May 09

## Re: C++ Do/While loop divisible by 11

Posted 04 October 2012 - 03:22 PM

Thanks mojo I appreciate it!

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}