Click here to Skip to main content
15,901,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

I saw here someone posted the same question but they are using C and I have to use C++.

Below is what I have but it not implementing the formula:
C++
#include using namespace std;
int main()
{
	int num;
	int divis;
	int sum;
	cout << "Enter a positive integer:";
		cin >> num;
	cout << endl;
	divis = num;
	sum = 0;
	do
	{ 
		sum = sum - num %10;
		num = num / 10;
		sum = sum + num %10;
		num = num/10;
	}
while (num >0);
cout <<"The sum of the integers:" << sum << endl;
if (sum % 11 == 0)
cout<< divis << " is divisible by 11" << endl;
else
cout << divis << " is not divisible by 11" <<endl;
}
Posted
Updated 31-Jan-11 19:59pm
v6
Comments
Nithin Sundar 1-Feb-11 0:14am    
I had to double back and read your question a few times to understand it properly.

For starters, I don't see much relation between your question and the program you have posted. Where's the function which was asked and where are you returning a boolean according to the requirement? And what's the "perfect number" supposed to mean?
ITHOPE 1-Feb-11 0:21am    
i was going to use a switch case function but i didntthink it would need it and i confused my self so would i use bool function and cmath?
Nithin Sundar 1-Feb-11 0:24am    
No you are confusing yourself here.

What is asked is a function written by YOU to do the task. You can use built in functions for your purpose but you should write a function which performs the task, in your case to check whether the number is divisible by 11 or not. THAT function should return a boolean value as "true" if the number was divisible and "false" if it wasn't.
ITHOPE 1-Feb-11 1:06am    
i just finished redoing it i will replace it now
Nithin Sundar 1-Feb-11 1:17am    
Refer my answer below first. You are still not doing the task in a function as stated in the question.

Your algorithm is slightly wrong. The first number is added, not subtracted. hence you need to swap the order of the + and - operations.

With that I seem to be getting the correct results

#include <iostream>

using namespace std;

int main() {
	int num;
	int divis;
	int sum;
	cout << "Enter a positive integer:";
	cin >> num;
	cout << endl;
	divis = num;
	sum = 0;
	do { 
		sum = sum + num % 10;
		num = num / 10;
		//This could go inside an if (num > 0), but it really makes no differance
		sum = sum - num % 10;
		num = num / 10;
	} while (num > 0);
	cout << "The sum of the integers: " << sum << endl;
	if (sum % 11 == 0) {
		cout << divis << " is divisible by 11" << endl;
	} else {
		cout << divis << " is not divisible by 11" << endl;
	}
}
 
Share this answer
 
I think you haven't understood the question. I can help you out with a basic layout. Try more and then let us know when you are stuck.

1. Before writing your program, understand the requirements first. If this step is skipped, then you will face numerous difficulties when you actually code.

2. Having understood the requirements, prepare your formulae or logic required. Write them down it helps.

3. By the time you start coding, you must have a thorough understanding of what you need to do or you will only end up writing random code or code which doesn't serve your purpose.

Here's a little pseudo code to help you get started:

#include (include your header files here)

//main function
int main ()
{
  //Accept user input;
  //Call a function and pass the input value;
  //Depending on the value returned show the output requested;
}

//Actual function
return_type function_name(enter the parameter variable here)
{
  //Do the required task and find out whether the number is divisible or not;
  //If divisible by 11 then return true as value else return false;
}


You can use this to start off.
 
Share this answer
 
v2
Andrew's answer uses operator %(11) which is probably unallowed in your exercise.
You must improve your code to handle various results from the algorithm:
C++
#include <cmath> // for std::abs()
bool divisible_by_11(int n)
{
    n = std::abs(n);
    while (n > 11)       // handle cases like 9191919
    {
        int n1 = 0;
        while (n)
        {
            n1 += n % 10;   // add last digit of n to n1
            n /= 10;        // divide n by ten
            n1 -=  n % 10;  // substract last digit of n to n1
            n /= 10;        // divide n by ten
        }
        n = (n1 == 0) ? 11 : std::abs(n1); // handle cases like 3333 (n1 == 0) or 91919191 (n1 < 0)
    }
    return n == 11;
}

If you want to return true on 0 you have to make some changes :)
cheers,
AR
 
Share this answer
 

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