Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please HELP me with my C++ homework, need be turn in tomorrow!!!!!!!!!!!!!!!! TH

ID: 3566258 • Letter: P

Question

Please HELP me with my C++ homework, need be turn in tomorrow!!!!!!!!!!!!!!!! THANKS!!!!!!!!!!!!!

Problem:

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 that n = 8784204, then, t = 4 - 0 + 2 - 4 + 8 - 7 + 8 = 11. Because 11 is divisible by 11, it follows that 6764204 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 C++ program that prompts the user to enter a positive integer and then uses this criterion to determine whether the number is divisible by 11. The following shows a scenario of running the program.

Enter a positive integer: 8784204

8784204 is divisible by 11

More integer (1 = yes, 2 = no)? 1

Enter a positive integer: 54063297

54063297 is not divisible by 11

More integer (1 = yes, 2 = no)? 2

Good-bye!

Hint: you need the following functions to solve the problem. (1) The string length function to determine the size of the string of digits. (2) The string at function to extract each of the digits (in character form). (3) The type cast function to convert each digit from char to int. (4) Subtract 48 from the converted digit (e.g.

Explanation / Answer

#include <iostream>

using namespace std;

int main()
{


int n;
//gets char at i th index
int i=0;
int sum=0;
int k=0;
/*
for(i=0;i<num.length();i++)
{char a = num.at(i);
if(i%2==0)
sum=sum+a;
else
sum=sum-a;
}*/
int p=1;
int n1;

while(p==1){
cout << "Enter a positive integer:" << endl;
cin>>n;
n1=n;
while(n>0)
{
k=n%10;
if(i%2==0)
sum=sum+k;
else
sum=sum-k;
  
n=n/10;
i++;
}
if(sum==11)
cout<<n1<<" is divisible by "<<sum<<endl;
else
cout<<n1<<" is not divisible by 11"<<endl;
  
cout<<"More integer (1 = yes, 2 = no)?"<<endl;
cin>>p;
}
cout<<"Good-bye!";
return 0;
}

o/p: