5 Replies - 2528 Views - Last Post: 04 October 2012 - 03:22 PM Rate Topic: -----

#1 khull1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is online

  • member icon


Reputation: 4140
  • View blog
  • Posts: 12,885
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 353
  • View blog
  • Posts: 774
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 khull1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 353
  • View blog
  • Posts: 774
  • 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.
Was This Post Helpful? 1
  • +
  • -

#6 khull1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1