Results 1 to 17 of 17
  1. #1
    denisdoherty is offline Member
    Join Date
    Apr 2008
    Posts
    31
    Rep Power
    0

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

  2. #2
    sal_manilla is offline Member
    Join Date
    Apr 2008
    Posts
    3
    Rep Power
    0

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

  3. #3
    denisdoherty is offline Member
    Join Date
    Apr 2008
    Posts
    31
    Rep Power
    0

    Default

    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. #4
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

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

    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 06:01 AM.
    freedom exists in the world of ideas

  5. #5
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

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

  6. #6
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    By the way, my codes above is just based on calculation's flow....
    freedom exists in the world of ideas

  7. #7
    denisdoherty is offline Member
    Join Date
    Apr 2008
    Posts
    31
    Rep Power
    0

    Default

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

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by denisdoherty View Post
    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. #9
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Quote Originally Posted by denisdoherty View Post
    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.
    sanjeev,संजीव

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  11. #11
    denisdoherty is offline Member
    Join Date
    Apr 2008
    Posts
    31
    Rep Power
    0

    Default

    No i can divide by anything i want to

  12. #12
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Quote Originally Posted by denisdoherty View Post
    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 12:10 PM.
    sanjeev,संजीव

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  14. #14
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    So he need to handle user inputs there, isn't Sanjeev?
    I think so Eranga, but there is no response.
    sanjeev,संजीव

  15. #15
    denisdoherty is offline Member
    Join Date
    Apr 2008
    Posts
    31
    Rep Power
    0

    Default

    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

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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?

  17. #17
    steven is offline Member
    Join Date
    Apr 2008
    Posts
    2
    Rep Power
    0

    Default

    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>

Similar Threads

  1. Need Help showing text in JTextArea
    By GuyFawkes in forum AWT / Swing
    Replies: 3
    Last Post: 05-05-2008, 10:19 AM
  2. Quotients/Remainders (Loops *Need Help*)
    By Zebra in forum New To Java
    Replies: 2
    Last Post: 04-17-2008, 04:08 PM
  3. Replies: 4
    Last Post: 04-16-2008, 02:26 PM
  4. Error Messages Not Showing Up
    By nvidia in forum Web Frameworks
    Replies: 0
    Last Post: 04-07-2008, 11:41 PM
  5. Why isn't this showing?
    By JToolTip in forum Java Applets
    Replies: 2
    Last Post: 07-08-2007, 12:54 AM

Posting Permissions

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