Hey Everyone,

I just have a quick question... does anyone know how to test 2 integers (int a, int b) to see if int a is evenly divisible by int b? I tried it like this but it doesn't really seem to work:

``````public boolean isDivisible(int a, int b)
{
double d = a / b;

if (d == Math.round(d))
return true;
else
return false;
}``````

Anyone have any ideas? Thanks in advance.

-Nick Nisi

## All 5 Replies

you can use "%"
if ((a % b) == 0 ) return true;

you can use "%"
if ((a % b) == 0 ) return true;

Thanks Tonakai!

- Nick Nisi

``````import java.io.*;
public class Divisibility{

public static void main(String[] args) throws IOException{

do

{

System.out.println("Enter start number:");
System.out.println("Enter end number:");

for (i= i; i<=j; i++){
System.out.println();
System.out.print(i+ " is divisible by ");
int d=1;
while (d<=j){
if (i%d == 0)
System.out.print(d+ " ");

d++;
}
}
System.out.println("\nEnter another number or 'no' to exit the program:");
}

}
}``````
commented: Trowing exception from main marks low skills and no passion. -4

Create a program that displays the numbers that divide evenly into all the numbers from a given starting and ending number by the user. Class name is Divisibility

Sample output

Starting number:1
Ending number: 1

1 is divisible by 1
2 is divisible by 1 2
3 is divisible by 1 3
.
.
.
.
.
77 is divisible by 1 7 11
78 is divisible by 1 2 3 6 13 26 39
.
.
.
.
100 is divisible by 1 2 4 5 10 20 25 50

Enter another number or 'no' to exit the program: no

Create a program that displays the numbers that divide evenly into all the numbers from a given starting and ending number by the user. Class name is Divisibility

Sample output

Starting number:1
Ending number: 1

1 is divisible by 1
2 is divisible by 1 2
3 is divisible by 1 3
.
.
.
.
.
77 is divisible by 1 7 11
78 is divisible by 1 2 3 6 13 26 39
.
.
.
.
100 is divisible by 1 2 4 5 10 20 25 50

Enter another number or 'no' to exit the program: no