Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

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

1. ## 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.

```[FONT=Arial Black][SIZE=2]public class DivisionBy11{
public static void main(String[] args) throws IOException{

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]:(

}

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.  Reply With Quote

3. ## Re: My Division Problem

```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;

for(int i = tempNumber.length() - 1; i >= 0; i--){
total += (tempNumber.charAt(i)-48);
}else{
total -= (tempNumber.charAt(i)-48);
}
}

if(total == 11){
System.out.println("Yes.");
}else{
System.out.println("No.");
}
}
}```

Chris  Reply With Quote

4. ## Re: My Division Problem

Have you heard of the mod operator, as well as the parseInt method? Scanner also has a nextInt methd.
```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");
}```  Reply With Quote

5. ## Re: My Division Problem

i was guessing it was a challenege set, where they cannot use mod to determine it  Reply With Quote

6. ## Re: My Division Problem calculate the remainder by hand (not completely sure how numerically stable this is, but in theory it works)
```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");
}```  Reply With Quote

7. ## The Following User Says Thank You to helloworld922 For This Useful Post:

Java'88 (August 16th, 2009)

8. ## Re: My Division Problem

Thank very very much for your help Freaky Chris and hellooworld922.   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. 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);
}

...  Reply With Quote

9. ## 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  Reply With Quote

10. ## The Following User Says Thank You to Freaky Chris For This Useful Post:

Java'88 (August 16th, 2009)

11. ## Re: My Division Problem 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   Reply With Quote

12. ## Re: My Division Problem

Yeah I agree that mod should not be used, because that is what assignment was all about.

Acumen
Lucid forums  Reply With Quote