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

Let n be an integer and t= .Its known that n is divisible by 11 if and only if t

ID: 675081 • Letter: L

Question

Let n be an integer and t= .Its known that n is divisible by 11 if and only if t is divisible by 11.

Please write a C program using the instructions below. The program can't use arrays, ans mut only use <stdio.h>

(20%) (Divisible by 11) Let n-akak-lak-2 alao be an integer and t= a,- a1 taz-+-1)*a. It is known that n is divisible by 11 if and only if t is a, + a,-...+(-1)"ak. 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 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 program that prompts the use to enter a positive integer and then uses this criterion to determine whether the number is divisible by 11. Here is a sample input/output dialog: >/hw1-2 Enter n: 8784204 8784204 is divisible by 11.

Explanation / Answer

#include <stdio.h>
int digitCounter(long num)
{
int count = 0;
while(num != 0)
{
count++;
num /= 10;
}
return count;
}

int isDivisibleByEleven(long num)
{
int sum = 0, rem, sign, i;
int numOfDigits;
numOfDigits = digitCounter(num);
for(i = 0; i < numOfDigits; i++)
{
if(i %2 == 0)
sign = 1;
else
sign = -1;
rem = num % 10;
num /= 10;
sum += (sign * rem);
}
if(sum % 11)
return 0;
return 1;
}

int main()
{
long num;
printf("Enter n: ");
scanf("%li",&num);
if(isDivisibleByEleven(num))
printf("%li is divisible by 11. ", num);
else
printf("%li is not divisible by 11. ", num);
}