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

C++ programming • Write the body of a function with the following signature. boo

ID: 3910988 • Letter: C

Question

C++ programming • Write the body of a function with the following signature. bool divisible_11(int n, int &s); • You are given an input positive integer n > 0. • The decimal digits of n are x1x2x3 . . . . • The sum of the digits in the odd locations is s1 = x1 + x3 + . . . . • The sum of the digits in the even locations is s2 = x2 + x4 + . . . . • Set s to the absolute difference s = |s1 ? s2|. • If s == 0 then return true. • Else if s < 11 return false. • If s ? 11 then set n = s and call the function recursively until you obtain 0 ? s < 11. 1. For example if n = 65432 then s1 = 6 + 4 + 2 = 12 and s2 = 5 + 3 = 8. The absolute difference is |s1 ? s2| = 12 ? 8 = 4 hence return false. 2. For example if n = 7909 then s1 = 7+0 = 7 and s2 = 9+9 = 18. The absolute difference is |s1 ? s2| = |7 ? 18| = 11 hence proceed recursively. The new values are s1 = 1 and s2 = 1, hence |s1 ? s2| = |1 ? 1| = 0 hence return true. • If you do your work correctly, the function will return true if n is divisible by 11 (and the output will be s = 0) and false if not (and the output will be a value 1 ? s ? 10). • Apply your function to your student id and state the output

Explanation / Answer

Below is the body of the function bool divisible_11(int n, int &s). You can apply this to your student id.

bool divisible_11(int n, int &s)
{
int i =0;
int s1 = 0;
int s2 = 0;
while(n!=0)
{
i++;
if(i%2==0)
{
s2 += n%10;
}
else
{
s1 += n%10;
}
n = n/10;
}
s = abs(s1-s2);
if(s==0)
return true;
else if(s<11)
return false;
else
return divisible_11(s, s);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote