Click here to Skip to main content
16,023,047 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I Need Help Concerning My C++ Program.
In Need To Write A Program That Checks Whether An Integer Is Divisible By 11 Or Not. Here Is A Copy Of My Code. It Runs Without Errors Though It Gives Wrong result

#include 
#include 
using namespace std;
int n, a, power=0, t=0;
bool divisible (int)
{
	a = abs (n);
	if (a<11)
		return false;
	else if (a=11)
		return true;
	else 
		while ((a/(static_cast<removed int="">(pow(10.0, power)))>=1))
		{
			
			if (((a/(static_cast<removed int="">(pow(10.0, power))))<10))
				switch (power % 2)
			{
				case 0:
				t+=(a/(static_cast<removed int="">(pow(10.0, power))));
				break;
				case 1:
				t-=(a/(static_cast<removed int="">(pow(10.0, power))));
				break;
			}
			
			else 
				switch (power % 2)
			{
				case 0:
				t += ((a/(static_cast<removed int="">(pow(10.0, power))))%10);
				break;
				case 1:
				t-= ((a/(static_cast<removed int="">(pow(10.0, power))))%10);
				break;
			}
			
			power++;
		}
		if (t%11 == 0)
			return true;
		else if (divisible (t) != 0)
			return false;
		
		
}


void main ()
{ 
	cout<<"Enter an integer \n";
	cin>>n;
	switch (divisible (n))
	{
	case 1: cout<<"The number "<<n<<" is divisible by 11 \n";
		break;
	case 0: cout<<"The number "<<n<<" is not divisible by 11 \n";
		break;
	}
}


[Further description of problem posted as reply pasted here]
The question requires us to use the specified formula t = a0 -a1 + a2 - a3...+(-1)kak.. so i would appreciate it if you rather check my posted code .. thanks

The question is:

1. Let n = akak-1ak-2…a1a0 be an integer and t = a0 – a1 + a2 - … + (-1)kak. It is known that n is divisible by 11 if and only if t is divisible by 11. For example, suppose n = 8784204. Then t = 4 – 0 + 2 – 4 + 8 – 7 + 8 = 11. Since 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 function that takes as input an integer and that return a Boolean indicating whether the integer is divisible or not by 11. Write a program to test your function.
Posted
Updated 25-Mar-10 4:58am
v3

"It Gives Wrong result" isn't descriptive nor informative. You could have given examples of cases that work well and others that don't.
You could have said "It almost always returns true".

And you could have found a pattern yourself.

"else if (a=11)..."
Ah the good old operator traps provided by C and C++.

:)
 
Share this answer
 
A (very) slow solution :-D :
int TakeByIndex(int iTest, int iIndex)
{
  return (iTest % pow(10, iIndex + 1)) / pow(10, iIndex); 
}
 

bool IsDivisibleBy11(int iTest)
{
  int a = 1,
      k = 1,
      i = 0,
      s = 0;
 

  while (k && a) {
    a = TakeByIndex(iTest, i++);
    k = TakeByIndex(iTest, i++);
    s += (a - k);
  }
 

  return (0 == s % 11);
}
 
Share this answer
 
v3
Try it :) :
bool IsDivisibleBy11(int iTest)
{
  return (0 == iTest % 11);
}
 
Share this answer
 
Thank you very much Luc Pattyn! :-D it worked.. I really appreciate your help
 
Share this answer
 
[Moved the question posted as answer to the original question]

To 7mesho : Please don't add your reply as answer. Modify the question itself
 
Share this answer
 
v3

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



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