# Thread: Dividing numbers with remainders showing

1. Member
Join Date
Apr 2008
Posts
31
Rep Power
0

## Dividing numbers with remainders showing

I am looking to Dividing numbers within java. the code that i have at the minute is

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

Java 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);
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?

2. Member
Join Date
Apr 2008
Posts
3
Rep Power
0

## Please describe in words what you want to do.

3. Member
Join Date
Apr 2008
Posts
31
Rep Power
0
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.

4. 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

Java 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);```
Last edited by sukatoa; 04-24-2008 at 05:01 AM.

5. Use doubles or floats. the int data type is exactly that an Integer. Doubles have some decimal places and floats have even more.

6. By the way, my codes above is just based on calculation's flow....

7. Member
Join Date
Apr 2008
Posts
31
Rep Power
0
I know but as i said i can enter what ever numbers i like at the start. the numbers will not be predefined

8. 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.

9. 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.

10. Member
Join Date
Apr 2008
Posts
31
Rep Power
0
No i can divide by anything i want to

11. Originally Posted by denisdoherty
No i can divide by anything i want to
So it will be also entered by user or anything else....
Last edited by sanjeevtarar; 04-24-2008 at 11:10 AM.

12. Originally Posted by Eranga
So he need to handle user inputs there, isn't Sanjeev?
I think so Eranga, but there is no response.

13. Member
Join Date
Apr 2008
Posts
31
Rep Power
0
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

14. 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?

15. Member
Join Date
Apr 2008
Posts
2
Rep Power
0
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>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•