2

I can only seem to get it to display the quotient and the decimal. For example if I divide 9 by 2, I will get 4.5. However, I would like to get it to be 4r 1. My current code is import java.util.Scanner;

public class Convert{   

 public static void main(String [] args){      
  Scanner reader = new Scanner(System.in);      

  double dividend;      
  double divisor;      
  double quotient;           

  System.out.print("Enter the dividend ");    
  dividend = reader.nextDouble();     

  System.out.print("Enter the divisor ");       
  divisor = reader.nextDouble();    

  quotient = dividend/divisor;          

  System.out.print("The quotient is ");    
  System.out.println(quotient);       
 } 
}
GhostCat
  • 137,827
  • 25
  • 176
  • 248
Burk
  • 23
  • 3

3 Answers3

4

Simple: you are using the wrong types then.

It seems that you want to operate on whole numbers.

Then don't use floating point numbers, but types such as int or long.

Because different types imply different semantics for the corresponding operations, see here for example. Beyond that, you then want to look into the % or modulo operator (see there).

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

First off, don't use a decimal type like float or double, use a whole-number type like int or long. Second off, to get a reminder, you'll need to use the modulus operator (%). It works like so:

int i = 5 % 2; equals 1.

int i = 13 % 7; equals 6.

ack
  • 1,181
  • 1
  • 17
  • 36
0

You could change all values to an int

Then also get the remainder by using the modulus operator (%)

int dividend;
int divisor;
int quotient;
int remainder;
String remainderResult;

System.out.print("Enter the dividend ");
dividend = reader.nextInt();

System.out.print("Enter the divisor ");
divisor = reader.nextInt();

quotient = dividend / divisor;
remainder = dividend % divisor;

remainderResult = (remainder != 0) ? ", remainder " + remainder : "";

System.out.println("The quotient is " + quotient + remainderResult);
achAmháin
  • 4,176
  • 4
  • 17
  • 40