# Dividing numbers with remainders showing

• 04-23-2008, 05:09 PM
denisdoherty
Dividing numbers with remainders showing
I am looking to Dividing numbers within java. the code that i have at the minute is

Code:

```public synchronized void setProduct (int number)       {             this.product = (number / number);       }```
and also

Code:

```if (selection==2)  { // Returing the sum of the numbers             int i = 1;             int number = 0;             int sendNum = 0;             do             {                 System.out.println("Enter Number: " + i);                 String num = br.readLine();                 sendNum = Integer.parseInt(num);                 stub.setProduct(sendNum);                 i++;                 }while(i<=4);                 number = stub.getProduct();                 System.out.println("NUMBER " + number);             }//end if```
I was wondering does anyone know if i am in the right direction or what way do i have to do it?
• 04-23-2008, 10:25 PM
sal_manilla
Please describe in words what you want to do.
From your code it's not clear what your program should do. Please describe your problem(s) in words.
• 04-23-2008, 11:59 PM
denisdoherty
I want the program to be able to divide numbers like 100/2/2/2 and then have it out put 12.5. I want to input the numbers tho and they will not be the same every time.
• 04-24-2008, 04:58 AM
sukatoa
12.5? where is the remainder? 5 in .5 is a decimal number....

100/2 = 50
50/2 = 25
25/2 = 12.5

12 * 2 = 24
25 - 24 = 1

Your remainder is 1...

Code:

```int dividend = 100; int divisor = 2; int times = 3; int start = 0; while(start<(times-1)){       dividend /= divisor;       start++; } int final_quotient = 0; while(dividend>=divisor){       final_quotient++;       dividend -= divisor; } System.out.println("Quotient: "+final_quotient+" Remainder: "+dividend);```
• 04-24-2008, 08:39 AM
Zosden
Use doubles or floats. the int data type is exactly that an Integer. Doubles have some decimal places and floats have even more.
• 04-24-2008, 09:44 AM
sukatoa
By the way, my codes above is just based on calculation's flow....
• 04-24-2008, 09:55 AM
denisdoherty
I know but as i said i can enter what ever numbers i like at the start. the numbers will not be predefined
• 04-24-2008, 10:23 AM
Eranga
Quote:

Originally Posted by denisdoherty
I know but as i said i can enter what ever numbers i like at the start. the numbers will not be predefined

You want to get data from the user? Used Scanner for that. There are lots of way to do it.
• 04-24-2008, 10:27 AM
sanjeevtarar
Quote:

Originally Posted by denisdoherty
I want the program to be able to divide numbers like 100/2/2/2 and then have it out put 12.5. I want to input the numbers tho and they will not be the same every time.

Do you need to divide the number always by 2.
• 04-24-2008, 10:31 AM
Eranga
I think so that Sanjeev. Seems that Sukatoa try to do the same thing. :)
• 04-24-2008, 10:37 AM
denisdoherty
No i can divide by anything i want to
• 04-24-2008, 11:01 AM
sanjeevtarar
Quote:

Originally Posted by denisdoherty
No i can divide by anything i want to

So it will be also entered by user or anything else....
• 04-24-2008, 11:07 AM
Eranga
So he need to handle user inputs there, isn't Sanjeev?
• 04-24-2008, 11:11 AM
sanjeevtarar
Quote:

Originally Posted by Eranga
So he need to handle user inputs there, isn't Sanjeev?

I think so Eranga, but there is no response.
• 04-24-2008, 11:27 AM
denisdoherty
I got it working my self. All i did was this.average += number;
this.average2 = average /4;

this allowed me to input any numbers that i like and also return numbers
• 04-24-2008, 11:36 AM
Eranga
I'm not clear what exactly you want to do. Are you try to find the sum of few numbers and then find the average? If so, how did you change the number of values?
• 04-24-2008, 05:39 PM
steven
The math package has divideAndRemainder() that does this. I made a short example, inputs are strings so you may have to jump through a few hoops.

<code>
import java.math.*;

public class divideWithRemainder
{
public static void main (String [] args){
// Create via a string, other options are available
BigDecimal num1 = new BigDecimal("1137");
BigDecimal num2 = new BigDecimal("83");

// an array to hold the result
BigDecimal[] num3 = new BigDecimal[2];

System.out.println ("divide " + num1 + " by " + num2 + " return both result and remainder");

num3 = num1.divideAndRemainder(num2);
System.out.println ("quotient = " + num3[0] + " remainder = " + num3[1]);
}
}
</code>