Hi all, first time user on this website and I'm very new to Java so I'm kind of stuck on this I would appreciate any help I can get. My assignment is as follows and I got everything except one last part that I cant figure out how to write it. It deals with the remainder operator.

Write a program (DivideTwo.java) that does the following: allows a user to enter two integers; divides the two; and prints one of two messages based on the result.
IF the first divided by the second is a whole number (remainder is zero) print:
Sample program run
14 divided by 7 is: 2

Otherwise print:
Sample program run
23 divided by 4 is: 5 with a remainder of 3
23 divided by 4 is: 5.75

Attachments
``````/**
* Name: Dave Szura
* Date: October 21 2008
* Section #: 2
* Lab #: 08
*/

import java.util.Scanner; //Needed for the Scanner class
import java.text.DecimalFormat;

public class DivideTwo {

public static void main(String[] args) {

double number1; //First number that the user will enter
double number2; //Second number that the user will enter

//Create a Scanner object to read input
Scanner keyboard = new Scanner(System.in);
//Create a DecimalFormat object
DecimalFormat formatter = new DecimalFormat("#0.00");

//Get two numbers from the user
number1 = keyboard.nextDouble();
number2 = keyboard.nextDouble();

//Divide the two numbers
double number3 = (number1 / number2);

if(number1 % number2 == 0)
{
System.out.println(number1 + " " + "divided by" + " " + number2 + " " + "is:" + " " + number3);
}

if(number1 % number2 != 0)

{
System.out.println(number1 + " " + "divided by" + " " + number2 + " " + "is:" + " " + xxx + "with a remainder of" + xxx);
System.out.println(number1 + " " + "divided by" + " " + number2 + " " + "is:" + " " + number3);
}
}
}``````
2
Contributors
2
Replies
4
Views
9 Years
Discussion Span
Last Post by neighbordave

So the part you are having trouble with is the bold section? If it is think about what modulus ( % ) does. It finds the remainder in division. You need the largest whole number that fits, plus the remainder. So, what I would do is create an value that is equal to the remainder

``double remainder = (number1 % number2);``

That will be your remainder and if you cast number3 to an int

``(int) number3``

than you have the largest whole number that fits and the remainder.