Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: division without division operator

  1. #1
    Junior Member
    Join Date
    Oct 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default division without division operator

    hello i started learning how to program this week and i am not able to complete this exercise you can see what is wrong in the code

    static int division (int a, int b) {
    int c= a-b;
    while (c <= b) {
    c = c - b;
    }
    if (c < b) {
    return c + b; }

    else {

    return c;

    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: division without division operator

    i am not able to complete this exercise
    Please explain what the problem is.
    If there are error messages, copy the full text and paste it here.
    If the output is not what you want, post the output and explain what is wrong and show what you want instead.

    Also please add comments to the code describing what it is trying to do and how it is going to do it.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: division without division operator

    Quote Originally Posted by Norm View Post
    Please explain what the problem is.
    If there are error messages, copy the full text and paste it here.
    If the output is not what you want, post the output and explain what is wrong and show what you want instead.

    Also please add comments to the code describing what it is trying to do and how it is going to do it.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    Thanks for reply. I need the function to divide two numbers without the division operator and so I am using subtraction method, but when I perform the function I for example choose the number 31 for variable "a" which will divide by variable "b" which for example I choose 5. but the function only subtracts once but by the cycle I made it should subtract until it is not below the variable "b". The result of 31 divided by 5 gave 26.

    Here is the code:

    static int division (int a, int b) {
    		int c= a-b;
    		while (c <= b) {
    			c = c - b;
    		}
    		if (c < b) {
    			return c + b; }
     
    		else {
     
    			return c;
     
    		}

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: division without division operator

    Please use better variable names in the code. Variable names should describe what the variable contains. a,b,c do not describe what they contain. Better names would be dividend, divisor, quotient and remainder.

    Should the method count the number of times a subtraction is made?
    What is in c when the method exits?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: division without division operator

    Should the method count the number of times a subtraction is made? no need to count, just subtract the divisor and dividend until the result is no less than the dividend

    What is in c when the method exits? a variable c is the result of subtraction


    31/5=6 and 31-5=26, 26-5=21, 21-5=16, 16-5, 11-5=6, and this I want the function to do.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: division without division operator

    Ok, can you rename the variables to show which one is the dividend, the divisor, the quotient and the remainder?

    Can you describe in English how the program will compute the quotient and the remainder?
    You need a description of the steps the program should take to solve the problem before writing the code.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: division without division operator

    i already solve thanks for help.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: division without division operator

    no need to count
    Were you able to solve the problem without counting?
    If so can you post the code that shows how you did it?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Division by zero exception
    By arnavkumartechno in forum Exceptions
    Replies: 3
    Last Post: February 3rd, 2014, 06:52 AM
  2. [SOLVED] Division and lots of 0's?
    By sci4me in forum Java Theory & Questions
    Replies: 0
    Last Post: July 25th, 2013, 11:35 PM
  3. division
    By ericgomez in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 23rd, 2013, 10:15 AM
  4. Division by Zero
    By mael331 in forum Java Theory & Questions
    Replies: 16
    Last Post: December 6th, 2011, 06:13 PM
  5. [SOLVED] Java program to write a code to detect divisibility of any number by 11
    By Java'88 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: August 11th, 2009, 10:41 PM