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

import java.io.*;
public class Divisibility{
	
	
	public static void main(String[] args) throws IOException{
		
	String answer;
 	do
		
	{
		
		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
		
	
	
			System.out.println("Enter start number:");
			int i = Integer.parseInt(input.readLine());			
			System.out.println("Enter end number:");
			int j = Integer.parseInt(input.readLine());
		
			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:");
  	answer = input.readLine();
  }
  while (!answer.equals ("no"));	
	
		}
	}

Edited 4 Years Ago by peter_budo: Keep It Clear - Do use code tags when posting any code

Comments
Trowing exception from main marks low skills and no passion.

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

Read the rule's before posting
Member rules
Create you own thread
No one's gonna do all the work for you, post your code to show your effort and we'll help if you have problems with it

Edited 4 Years Ago by zeroliken: n/a

This article has been dead for over six months. Start a new discussion instead.