0

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

4
Contributors
5
Replies
7
Views
12 Years
Discussion Span
Last Post by zeroliken
-1
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 by peter_budo: Keep It Clear - Do use code tags when posting any code

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

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

0

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 by zeroliken: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.