Java program to write a code to detect divisibility of any number by 11

• August 7th, 2009, 07:35 AM
Java'88
Java program to write a code to detect divisibility of any number by 11
Introduction to my problem:

I have to code a program that ask for the user to enter a positive integer and then I have to find if the value that was enter is divisible by 11 (with no remainder) and display "yes" if it is divisible and "no" if it is not.

However, the method that I have to use to determine if the enter value is divisible by 11 (with no remainder) is by using a certain method demonstrated below:

For example suppose that n=9343752, then t=2-5+7-3+4-3+9==11. 11 is
divisible by 11 then it follows that 8784204 is divisible by 11.

If n=843439 then t=9-3+4-3+4-8=3. 3 is not divisible by 11 then
843439 is not divisible by 11.

if the user enter's a non postive integer the program should continue asking for a positive value until
the user does so.

My code so far:

My code below accepts only 10-digit integers and I have struggled for it to accept any postive integers. It will not run if enter a negative 10-digit integer.

Code :

```[FONT=Arial Black][SIZE=2]public class DivisionBy11{ public static void main(String[] args) throws IOException{ InputStreamReader in = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(in); System.out.println("Please enter a positive integer");   String numbers = br.readLine(); while( Keyboard.ReadInt() <0){[COLOR=Lime] /*I'm try to check if the enter value is a positive integer and if it's not, to prompt the user to enter a positive integer. So I've been unsuccessful.*/[/COLOR]:(   System.out.println("Please enter a positive integer");   }   String numbers1 = numbers.substring(9,10); String numbers2 = numbers.substring(8,9); String numbers3 = numbers.substring(7,8); String numbers4 = numbers.substring(6,7); String numbers5 = numbers.substring(5,6); String numbers6 = numbers.substring(4,5); String numbers7 = numbers.substring(3,4); String numbers8 = numbers.substring(2,3); String numbers9 = numbers.substring(1,2); String numbers10 = numbers.substring(0,1); int numberi1 = Integer.parseInt(numbers1); int numberi2 = Integer.parseInt(numbers2); int numberi3 = Integer.parseInt(numbers3); int numberi4 = Integer.parseInt(numbers4); int numberi5 = Integer.parseInt(numbers5); int numberi6 = Integer.parseInt(numbers6); int numberi7 = Integer.parseInt(numbers7); int numberi8 = Integer.parseInt(numbers8); int numberi9 = Integer.parseInt(numbers9); int numberi10 = Integer.parseInt(numbers10); System.out.println("The number was " + numbers); float t = 0; float n=0; int myvalue = 0; t = numberi1 - numberi2 + numberi3 - numberi4 + numberi5 - numberi6 + numberi7 - numberi8 + numberi9 - numberi10; System.out.println(t); n = t / 11;   if(Math.round(n)==myvalue){ [COLOR=Lime]//I'm having difficulty in check the return value is an integer[/COLOR]:-ss System.out.println("Yes"); } else System.out.println("No");     }   }[/SIZE][/FONT]```

Any help will be greatly appreciated.
• August 7th, 2009, 08:32 AM
Freaky Chris
Re: My Division Problem
Code :

```import java.util.Scanner;     public class DivideBy11 { public static void main(String[] args){ int inputNumber;   System.out.println("Enter a positive number: "); while((inputNumber = new Scanner(System.in).nextInt()) < 0);   String tempNumber = Integer.toString(inputNumber); int total = 0; boolean add = true;   for(int i = tempNumber.length() - 1; i >= 0; i--){ if(add){ total += (tempNumber.charAt(i)-48); add = false; }else{ total -= (tempNumber.charAt(i)-48); add = true; } }   if(total == 11){ System.out.println("Yes."); }else{ System.out.println("No."); } } }```

Chris
• August 7th, 2009, 08:58 AM
helloworld922
Re: My Division Problem
Have you heard of the mod operator, as well as the parseInt method? Scanner also has a nextInt methd.
Code :

```Scanner input = new Scanner(System.in); System.out.print("Enter a number: "); int num = input.nextInt(); if (num % 11 == 0) { System.out.println(num + " is divisible by 11"); } else { System.out.println(num + " has a remainder of " + (num%11) + " when divided by 11"); }```
• August 7th, 2009, 09:21 AM
Freaky Chris
Re: My Division Problem
i was guessing it was a challenege set, where they cannot use mod to determine it
• August 8th, 2009, 11:43 AM
helloworld922
Re: My Division Problem
:P calculate the remainder by hand (not completely sure how numerically stable this is, but in theory it works)
Code :

```Scanner input = new Scanner(System.in); System.out.print("Enter a number: "); double num = input.nextInt(); if (num - (11 * Math.floor(num/11)) == 0) { System.out.println(num + " is divisible by 11"); } else { int remainder = (int)(num - 11 * Math.floor(num/11)); System.out.println(num + " has a remainder of " + remainder + " when divided by 11"); }```
• August 8th, 2009, 01:42 PM
Java'88
Re: My Division Problem
Thank very very much for your help Freaky Chris and hellooworld922. :-bd:-bd

Quote:

Originally Posted by Freaky Chris
i was guessing it was a challenege set, where they cannot use mod to determine it

Yes, mod operater was not allowed, thats what made this challenge really difficult for me.

The method below had to be use as that is what the challenge ask for.

Method:
E.g. n=9343752, then t=2-5+7-3+4-3+9==11. 11 is
divisible by 11 then it follows that 8784204 is divisible by 11.

Quote:

Originally Posted by helloworld922
Have you heard .... parseInt method? ..Scanner also has a nextInt methd.

I have not yet reach the parseInt and Scanner method in my Java for Dummies, 4th edition:)

Freaky Chris would example to me how this part of your code works, as I did not understand fully what is happening here.

Thank you.

JAVA CODE
...
total += (tempNumber.charAt(i)-48);
}else{
total -= (tempNumber.charAt(i)-48);
}

...
• August 8th, 2009, 03:58 PM
Freaky Chris
Re: My Division Problem
subractign 48 from the character representation of anumber gives you the numerical value of it for example, '1'-48 = 1 if that makes sense

Chris
• August 9th, 2009, 07:02 AM
Java'88
Re: My Division Problem
Quote:

Originally Posted by Freaky Chris
subractign 48 from the character representation of anumber gives you the numerical value of it for example, '1'-48 = 1 if that makes sense

Chris

Yes, that make sense for me, Thanks :D
• August 11th, 2009, 10:41 PM
trueacumen
Re: My Division Problem
Yeah I agree that mod should not be used, because that is what assignment was all about.

Acumen
Lucid forums