Question:

How to java programming?

by  |  earlier

0 LIKES UnLike

Let n = akak-1ak-2…a1a0 be an integer. Let 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 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 user to enter the positive integer and then use this criterion to determine whether the number is divisible by 11.

---------------------

i got some idea but i don't know how to use this below code to solve above question tell me please >>> URGENTLY

[CODE]

private boolean IsDivisibleByEleven(intnumber)

{

char[] digits = number.toString().ToCharArray();

int total = Integer.parseInt(digits[digits.Length-1]...

for (int i = digits.length - 2; i >=0; i--)

{

if (i % 2 == 0)

{

total += Integer.parseInt(digits[i].toString());

}

else

{

total -= Integer.parseInt((digits[i].toString());

}

}

return total % 11 == 0;

}

[/CODE]

 Tags:

   Report

1 ANSWERS


  1. I'm guessing your solution works for only numbers that have even number of digits. That's because your are conditionally adding or subtracting based on the iteration of the loop which changes based on the length of the number.

    If the number you enter is even then your first operation will be to subtract which is correct. But if the number is of odd length then the first operation will be to add which is not correct.

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.